{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c54a3bbd",
   "metadata": {},
   "outputs": [],
   "source": [
    "from math import sqrt\n",
    "import copy\n",
    "import  traceback\n",
    "import shutil\n",
    "\n",
    "import numpy as np  # linear algebra\n",
    "import pydicom\n",
    "import os\n",
    "import matplotlib.pyplot as plt\n",
    "import cv2\n",
    "from pydicom.uid import UID\n",
    "from PIL import Image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5f869762",
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_scan(path):\n",
    "    slices = [pydicom.dcmread(path + '/' + s) for s in filter(lambda x: x.endswith('.dcm'), os.listdir(path))]\n",
    "    slices.sort(key=lambda x: float(x.InstanceNumber))\n",
    "    return slices\n",
    "\n",
    "\n",
    "def get_pixels_hu(slices):\n",
    "    image = np.stack([s.pixel_array for s in slices])\n",
    "    # Convert to int16 (from sometimes int16),\n",
    "    # should be possible as values should always be low enough (<32k)\n",
    "    image = image.astype(np.int16)\n",
    "\n",
    "    # Set outside-of-scan pixels to 0\n",
    "    # The intercept is usually -1024, so air is approximately 0\n",
    "    # image[image <= -2000] = 0\n",
    "\n",
    "    # Convert to Hounsfield units (HU)\n",
    "    for slice_number in range(len(slices)):\n",
    "        intercept = slices[slice_number].RescaleIntercept\n",
    "        slope = slices[slice_number].RescaleSlope\n",
    "        height, width = image[slice_number].shape[0], image[slice_number].shape[1]\n",
    "        \n",
    "        image[slice_number][image[slice_number]<=-2000] = image[slice_number, int(height*0.05), int(width*0.5)]\n",
    "        if slope != 1:\n",
    "            image[slice_number] = slope * image[slice_number].astype(np.float64)\n",
    "            image[slice_number] = image[slice_number].astype(np.int16)\n",
    "        image[slice_number] += np.int16(intercept)\n",
    "\n",
    "    return np.array(image, dtype=np.int16)\n",
    "\n",
    "\n",
    "class Elem():\n",
    "    def __init__(self, key, contour, t, out_of_range):\n",
    "        self.root = key\n",
    "        self.end = key\n",
    "        self.contours = [contour]\n",
    "        self.keys = [key]\n",
    "        self.t = t\n",
    "        self.out_of_range = out_of_range\n",
    "        \n",
    "    def __len__(self):\n",
    "        return len(self.contours)\n",
    "    \n",
    "    def append(self, key, contour, out_of_range):\n",
    "        self.end = key\n",
    "        self.contours.append(contour)\n",
    "        self.keys.append(key)\n",
    "        self.out_of_range = self.out_of_range or out_of_range\n",
    "        \n",
    "    def get_bboxes(self):\n",
    "        self.circles = [cv2.minEnclosingCircle(c) for c in self.contours]\n",
    "        def process(circle):\n",
    "            cx, cy, r = round(circle[0][0]), round(circle[0][1]), circle[1]\n",
    "            return cx - int(1.5*r), cy - int(1.5*r), cx + int(1.5*r), cy + int(1.5*r)\n",
    "            \n",
    "        self.bboxes = [process(c) for c in self.circles]\n",
    "        \n",
    "    def __str__(self):\n",
    "        return str(self.root)+'->'+str(self.end)+':'+str(self.keys)\n",
    "    \n",
    "    def get_prev_area(self):\n",
    "        return cv2.contourArea(self.contours[-2])\n",
    "    \n",
    "    @staticmethod\n",
    "    def calc_ratio(proportion, kind=0, lower=0.08, upper=0.12):\n",
    "        if proportion >= upper:\n",
    "            return 1 if kind == 0 else 1 if kind == 1 else 1.2\n",
    "        elif proportion <= lower:\n",
    "            return 3 if kind == 0 else 8 if kind == 1 else 1.5\n",
    "        else:\n",
    "            if kind == 0:\n",
    "                return 2*sqrt((upper-proportion)/(upper-lower))+1 \n",
    "            elif kind == 1:\n",
    "                return (upper-proportion)/(upper-lower)*7+1\n",
    "            else:\n",
    "                return 0.3*sqrt((upper-proportion)/(upper-lower))+1.2\n",
    "    \n",
    "    def post_process(self, patient_pixels, start_x, start_y):       \n",
    "        self.post_bboxes = []\n",
    "        img_height, img_width = patient_pixels.shape[1:3]\n",
    "        start, end = self.root[0], self.end[0]+1\n",
    "        for i in range(start, end):\n",
    "            #cx, cy, r = round(self.circles[i-start][0][0]+start_x), round(self.circles[i-start][0][1]+start_y), int(self.circles[i-start][1])\n",
    "            cx = (self.bboxes[i-start][2]+self.bboxes[i-start][0])//2 + start_x\n",
    "            cy = (self.bboxes[i-start][3]+self.bboxes[i-start][1])//2 + start_y\n",
    "            r = int((self.bboxes[i-start][2]-self.bboxes[i-start][0]+1)/3)\n",
    "            sx, sy = max(0, int(cx-4.5*r)), max(0, int(cy-4.5*r))\n",
    "            ex, ey = min(img_width, int(cx+4.5*r+1)), min(img_height, int(cy+4.5*r+1))\n",
    "            gray = patient_pixels[i, sy:ey, sx:ex].astype(np.float32)\n",
    "            gray = np.clip(gray, 0, 100)\n",
    "            gray = (gray-gray.min())/(gray.max()-gray.min())*255\n",
    "            gray = gray.astype(np.uint8)\n",
    "            gray = cv2.medianBlur(gray, 7)\n",
    "            # gray = cv2.GaussianBlur(gray, (7,7), 2)\n",
    "            lap = cv2.Laplacian(gray, cv2.CV_16S)\n",
    "            gray = gray.astype(np.int16) - lap\n",
    "            gray = np.clip(gray, 0, 255).astype(np.uint8)\n",
    "            min_edge = min(img_height, img_width)\n",
    "            proportion = r*3 / min_edge\n",
    "            fc = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, dp=1, minDist=min(*gray.shape)//2, param1=200, param2=19, minRadius=r, maxRadius=int(min(min_edge*0.055,r*self.calc_ratio(proportion)+1)))\n",
    "            if fc is None:\n",
    "                #self.post_bboxes.append((self.bboxes[i-start][0]+start_x, self.bboxes[i-start][1]+start_y, self.bboxes[i-start][2]+start_x, self.bboxes[i-start][3]+start_y))\n",
    "                ratio = self.calc_ratio(proportion, kind=2)\n",
    "                m, n, p, q = cx - int(ratio*r), cy - int(ratio*r), cx + int(ratio*r), cy + int(ratio*r)\n",
    "                self.post_bboxes.append((m, n, p, q))\n",
    "#                 if 220 <= i <= 260:\n",
    "#                     print(f'{i}:put origin')\n",
    "            else:\n",
    "                if len(fc) != 1:\n",
    "                    raise Exception(f'len(fc) == {len(fc)}')\n",
    "                fc = fc[0]\n",
    "#                 if 220 <= i <= 260:\n",
    "#                     print(f'{i}:{fc}')\n",
    "                for circle in fc:\n",
    "                    fx, fy, fr = round(circle[0]+sx), round(circle[1]+sy), int(circle[2])\n",
    "                    assert fr >= r\n",
    "                    dis = sqrt((fx-cx)**2+(fy-cy)**2)\n",
    "#                     if 220 <= i <= 260:\n",
    "#                         print(f'{i}:fr:{fr},r:{r}|{0.5*(fr-r)}<{dis}<{fr - r/self.calc_ratio(proportion, 1)} and {(fr-r)/r}>0.1')\n",
    "                    if 0.5*(fr-r) < dis < fr - r/self.calc_ratio(proportion, 1) and (fr-r)/r > 0.1:\n",
    "                        m, n, p, q = fx-int(1.3*fr), fy-int(1.3*fr), fx+int(1.3*fr), fy+int(1.3*fr)\n",
    "                        self.post_bboxes.append((m, n, p, q))\n",
    "#                         if 220 <= i <= 260:\n",
    "#                             print(f'{i}:put new')\n",
    "                        break\n",
    "                else:\n",
    "                    #self.post_bboxes.append((self.bboxes[i-start][0]+start_x, self.bboxes[i-start][1]+start_y, self.bboxes[i-start][2]+start_x, self.bboxes[i-start][3]+start_y))\n",
    "                    ratio = self.calc_ratio(proportion, kind=2)\n",
    "                    m, n, p, q = cx - int(ratio*r), cy - int(ratio*r), cx + int(ratio*r), cy + int(ratio*r)\n",
    "                    self.post_bboxes.append((m, n, p, q))\n",
    "#                     if 220 <= i <= 260:\n",
    "#                         print(f'{i}:put origin')\n",
    "                    \n",
    "    def blur_bboxes(self, kind=0):\n",
    "        if kind == 0:\n",
    "            bboxes = self.bboxes\n",
    "        else:\n",
    "            bboxes = self.post_bboxes\n",
    "            \n",
    "        xs = [(x1+x2)//2 for x1, y1, x2, y2 in bboxes]\n",
    "        ys = [(y1+y2)//2 for x1, y1, x2, y2 in bboxes]\n",
    "        ws = [x2-x1+1 for x1, y1, x2, y2 in bboxes]\n",
    "        ksize, thresh, dis_thresh = 8, 0.20, 0.20\n",
    "        if len(bboxes) < 300:\n",
    "            ksize, thresh, dis_thresh = 5, 0.25, 0.25\n",
    "        for i in range(ksize, len(bboxes)-ksize):\n",
    "            mxs = sorted(xs[i-ksize: i+ksize+1])\n",
    "            mys = sorted(ys[i-ksize: i+ksize+1])\n",
    "            mws = sorted(ws[i-ksize: i+ksize+1])\n",
    "            cx, cy, cw = mxs[ksize], mys[ksize], mws[ksize]\n",
    "            if abs(cw-ws[i]) > cw * thresh:\n",
    "                r = cw//2\n",
    "                bboxes[i] = (cx-r, cy-r, cx+r, cy+r)\n",
    "            elif kind == 1:\n",
    "                dis = sqrt((cx-xs[i])**2+(cy-ys[i])**2)\n",
    "                if dis > cw * dis_thresh:\n",
    "                    r = cw//2\n",
    "                    bboxes[i] = (cx-r, cy-r, cx+r, cy+r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3a21ff42",
   "metadata": {},
   "outputs": [],
   "source": [
    "def draw_bbox(slices, path, start_x, start_y, branch, branch_start, branch_end, crown_start, aorta_end, save_path):\n",
    "    sl = save_path.split('/')\n",
    "    name = sl[-3] if sl[-1] != '' else sl[-4]\n",
    "    if not os.path.exists(save_path):\n",
    "        os.mkdir(save_path)\n",
    "    if not os.path.exists(os.path.join(save_path, 'picture')):\n",
    "        os.mkdir(os.path.join(save_path, 'picture'))\n",
    "    start = path.root[0]\n",
    "    for i, s in enumerate(slices):\n",
    "        img = s.pixel_array\n",
    "        intercept = slices[i].RescaleIntercept\n",
    "        slope = slices[i].RescaleSlope\n",
    "        if crown_start <= i < aorta_end:\n",
    "            x1, y1 = path.bboxes[i-start][0] + start_x, path.bboxes[i-start][1] + start_y\n",
    "            x2, y2 = path.bboxes[i-start][2] + start_x, path.bboxes[i-start][3] + start_y\n",
    "            picture = img[y1:y2+1, x1:x2+1].astype(np.int16)\n",
    "            if slope != 1:\n",
    "                picture = (slope * picture.astype(np.float64)).astype(np.int16)\n",
    "            picture += np.int16(intercept)\n",
    "            picture = np.clip(picture, -100, 500)\n",
    "            picture = ((picture+100)/600*255).astype(np.uint8)\n",
    "            picture = Image.fromarray(picture)\n",
    "            picture.save(os.path.join(save_path, f'picture/1_{name}_{i:04d}.png'))\n",
    "        if branch_start <= i < branch_end:\n",
    "            x1, y1 = branch.bboxes[i-start][0] + start_x, branch.bboxes[i-start][1] + start_y\n",
    "            x2, y2 = branch.bboxes[i-start][2] + start_x, branch.bboxes[i-start][3] + start_y\n",
    "            picture = img[y1:y2+1, x1:x2+1].astype(np.int16)\n",
    "            if slope != 1:\n",
    "                picture = (slope * picture.astype(np.float64)).astype(np.int16)\n",
    "            picture += np.int16(intercept)\n",
    "            picture = np.clip(picture, -100, 500)\n",
    "            picture = ((picture+100)/600*255).astype(np.uint8)\n",
    "            picture = Image.fromarray(picture)\n",
    "            picture.save(os.path.join(save_path, f'picture/2_{name}_{i:04d}.png'))\n",
    "        if path.root[0] <= i <= path.end[0]:\n",
    "            x1, y1 = path.bboxes[i-start][0] + start_x, path.bboxes[i-start][1] + start_y\n",
    "            x2, y2 = path.bboxes[i-start][2] + start_x, path.bboxes[i-start][3] + start_y\n",
    "            cv2.rectangle(img, (x1, y1), (x2, y2), 3000, 1)\n",
    "            if branch_start <= i < branch_end:\n",
    "                x1, y1 = branch.bboxes[i-start][0] + start_x, branch.bboxes[i-start][1] + start_y\n",
    "                x2, y2 = branch.bboxes[i-start][2] + start_x, branch.bboxes[i-start][3] + start_y\n",
    "                cv2.rectangle(img, (x1, y1), (x2, y2), 3000, 1)\n",
    "        s.PixelData = pydicom.encaps.encapsulate([img.tobytes()])\n",
    "        s.file_meta.TransferSyntaxUID = '1.2.840.10008.1.2.1'\n",
    "        s.save_as(os.path.join(save_path, f'{i}.dcm'), write_like_original=False)\n",
    "        \n",
    "def draw_post_bbox(slices, path, branch, branch_start, branch_end, crown_start, aorta_end, save_path):\n",
    "    sl = save_path.split('/')\n",
    "    name = sl[-3] if sl[-1] != '' else sl[-4]\n",
    "    if not os.path.exists(save_path):\n",
    "        os.mkdir(save_path)\n",
    "    if not os.path.exists(os.path.join(save_path, 'picture')):\n",
    "        os.mkdir(os.path.join(save_path, 'picture'))\n",
    "    start = path.root[0]\n",
    "    for i, s in enumerate(slices):\n",
    "        img = s.pixel_array\n",
    "        intercept = slices[i].RescaleIntercept\n",
    "        slope = slices[i].RescaleSlope\n",
    "        if crown_start <= i < aorta_end:\n",
    "            x1, y1 = path.post_bboxes[i-start][0], path.post_bboxes[i-start][1]\n",
    "            x2, y2 = path.post_bboxes[i-start][2], path.post_bboxes[i-start][3]\n",
    "            picture = img[y1:y2+1, x1:x2+1].astype(np.int16)\n",
    "            if slope != 1:\n",
    "                picture = (slope * picture.astype(np.float64)).astype(np.int16)\n",
    "            picture += np.int16(intercept)\n",
    "            picture = np.clip(picture, -100, 500)\n",
    "            picture = ((picture+100)/600*255).astype(np.uint8)\n",
    "            picture = Image.fromarray(picture)\n",
    "            picture.save(os.path.join(save_path, f'picture/1_{name}_{i:04d}.png'))\n",
    "        if branch_start <= i < branch_end:\n",
    "            x1, y1 = branch.post_bboxes[i-start][0], branch.post_bboxes[i-start][1]\n",
    "            x2, y2 = branch.post_bboxes[i-start][2], branch.post_bboxes[i-start][3]\n",
    "            picture = img[y1:y2+1, x1:x2+1].astype(np.int16)\n",
    "            if slope != 1:\n",
    "                picture = (slope * picture.astype(np.float64)).astype(np.int16)\n",
    "            picture += np.int16(intercept)\n",
    "            picture = np.clip(picture, -100, 500)\n",
    "            picture = ((picture+100)/600*255).astype(np.uint8)\n",
    "            picture = Image.fromarray(picture)\n",
    "            picture.save(os.path.join(save_path, f'picture/2_{name}_{i:04d}.png'))\n",
    "        if path.root[0] <= i <= path.end[0]:\n",
    "            x1, y1 = path.post_bboxes[i-start][0], path.post_bboxes[i-start][1]\n",
    "            x2, y2 = path.post_bboxes[i-start][2], path.post_bboxes[i-start][3]\n",
    "            cv2.rectangle(img, (x1, y1), (x2, y2), 3000, 1)\n",
    "            if branch_start <= i < branch_end:\n",
    "                x1, y1 = branch.post_bboxes[i-start][0], branch.post_bboxes[i-start][1]\n",
    "                x2, y2 = branch.post_bboxes[i-start][2], branch.post_bboxes[i-start][3]\n",
    "                cv2.rectangle(img, (x1, y1), (x2, y2), 3000, 1)\n",
    "        s.PixelData = pydicom.encaps.encapsulate([img.tobytes()])\n",
    "        s.file_meta.TransferSyntaxUID = '1.2.840.10008.1.2.1'\n",
    "        s.save_as(os.path.join(save_path, f'{i}.dcm'), write_like_original=False)\n",
    "        \n",
    "def output_image_and_label(slices, path, branch, branch_start, branch_end, crown_start, detection_end, save_path):\n",
    "    sl = save_path.split('/')\n",
    "    name = sl[-2] if sl[-1] != '' else sl[-3]\n",
    "    image_path = os.path.join(save_path, 'images')\n",
    "    label_path = os.path.join(save_path, 'labels')\n",
    "    if not os.path.exists(image_path):\n",
    "        os.mkdir(image_path)\n",
    "    if not os.path.exists(label_path):\n",
    "        os.mkdir(label_path)\n",
    "    with open(os.path.join(label_path, f'labels.txt'), 'w') as txt:\n",
    "        txt.write('aorta')\n",
    "    start = path.root[0]\n",
    "    for i, s in enumerate(slices):\n",
    "        img = s.pixel_array.astype(np.int16)\n",
    "        img_height, img_width = img.shape\n",
    "        intercept = slices[i].RescaleIntercept\n",
    "        slope = slices[i].RescaleSlope\n",
    "        if slope != 1:\n",
    "            img = (slope * img.astype(np.float64)).astype(np.int16)\n",
    "        img += np.int16(intercept)\n",
    "        img = np.clip(img, -100, 500)\n",
    "        img = ((img+100)/600*255).astype(np.uint8)\n",
    "        img = Image.fromarray(img)\n",
    "        img.save(os.path.join(image_path, f'{name}_{i:04d}.png'))\n",
    "        with open(os.path.join(label_path, f'{name}_{i:04d}.txt'), 'w') as txt:\n",
    "            if crown_start <= i < detection_end:\n",
    "                x1, y1 = path.post_bboxes[i-start][0], path.post_bboxes[i-start][1]\n",
    "                x2, y2 = path.post_bboxes[i-start][2], path.post_bboxes[i-start][3]\n",
    "                cx, cy = (x1+x2)//2, (y1+y2)//2\n",
    "                w, h = x2-x1+1, y2-y1+1\n",
    "                txt.write(f'0 {cx/img_width:.6f} {cy/img_height:.6f} {w/img_width:.6f} {h/img_height:.6f}')\n",
    "                if branch_start <= i < branch_end:\n",
    "                    x1, y1 = branch.post_bboxes[i-start][0], branch.post_bboxes[i-start][1]\n",
    "                    x2, y2 = branch.post_bboxes[i-start][2], branch.post_bboxes[i-start][3]\n",
    "                    cx, cy = (x1+x2)//2, (y1+y2)//2\n",
    "                    w, h = x2-x1+1, y2-y1+1\n",
    "                    txt.write(f'\\n0 {cx/img_width:.6f} {cy/img_height:.6f} {w/img_width:.6f} {h/img_height:.6f}')\n",
    "\n",
    "def adjust_gamma(img, gamma=1.0):\n",
    "    invGamma = 1.0 / gamma\n",
    "    table = np.array([((i / 255.0) ** invGamma) * 255 for i in np.arange(0, 256)]).astype(\"uint8\")\n",
    "    new_img = np.empty(img.shape)\n",
    "    new_img = cv2.LUT(img, table)\n",
    "    return new_img\n",
    "\n",
    "def get_intersection(origin, first, second):\n",
    "    zero1, zero2 = np.zeros_like(origin), np.zeros_like(origin)\n",
    "    cv2.fillPoly(zero1, [first], 125)\n",
    "    cv2.fillPoly(zero2, [second], 130)\n",
    "    inter = zero1 + zero2\n",
    "    inter[inter<255] = 0\n",
    "    contours, _ = cv2.findContours(inter, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)\n",
    "    inter_area = 0\n",
    "    for contour in contours:\n",
    "        inter_area += cv2.contourArea(contour)\n",
    "    first_area = cv2.contourArea(first)\n",
    "    second_area = cv2.contourArea(second)\n",
    "    if first_area == 0 or second_area == 0:\n",
    "        return 0\n",
    "    assert inter_area <= first_area and inter_area <= second_area\n",
    "    return max(inter_area/first_area, inter_area/second_area)\n",
    "\n",
    "def set_window00(image, w_center, w_width, crown_start, branch_start):\n",
    "    image_copy = image.copy()\n",
    "    image_copy = np.clip(image_copy, w_center-int(w_width/2), w_center+int(w_width/2))\n",
    "    image_copy = image_copy.astype(np.float32)\n",
    "    image_copy = ((image_copy - image_copy.min())/(image_copy.max() - image_copy.min())*255).astype(np.uint8)\n",
    "    for slice_number in range(len(image)):\n",
    "        min_l = (int(min(image_copy[slice_number].shape[0]*0.3, image_copy[slice_number].shape[1]*0.3))-1) // 2 * 2 + 1\n",
    "        \n",
    "        image_copy[slice_number] = cv2.GaussianBlur(image_copy[slice_number], (3,3), 1)\n",
    "        lap = cv2.Laplacian(image_copy[slice_number], cv2.CV_16S)\n",
    "        image_copy[slice_number] = np.clip(image_copy[slice_number].astype(np.int16) - lap, 0, 255).astype(np.uint8)\n",
    "        image_copy[slice_number] = cv2.adaptiveThreshold(image_copy[slice_number], 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, min_l, 0)\n",
    "        \n",
    "        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))\n",
    "        if crown_start <= slice_number <= branch_start+1:\n",
    "            image_copy[slice_number] = cv2.dilate(image_copy[slice_number], kernel, iterations=2)\n",
    "            image_copy[slice_number] = cv2.erode(image_copy[slice_number], kernel, iterations=7)\n",
    "            image_copy[slice_number] = cv2.dilate(image_copy[slice_number], kernel, iterations=3)\n",
    "        else:\n",
    "            image_copy[slice_number] = cv2.erode(image_copy[slice_number], kernel, iterations=5)\n",
    "            image_copy[slice_number] = cv2.dilate(image_copy[slice_number], kernel, iterations=3)\n",
    "    return image_copy\n",
    "\n",
    "def set_window01(image, w_center, w_width, crown_start, branch_start):\n",
    "    image_copy = image.copy()\n",
    "    image_copy = np.clip(image_copy, w_center-int(w_width/2), w_center+int(w_width/2))\n",
    "    image_copy = image_copy.astype(np.float32)\n",
    "    image_copy = ((image_copy - image_copy.min())/(image_copy.max() - image_copy.min())*255).astype(np.uint8)\n",
    "    for slice_number in range(len(image)):\n",
    "        min_l = (int(min(image_copy[slice_number].shape[0]*0.3, image_copy[slice_number].shape[1]*0.3))-1) // 2 * 2 + 1\n",
    "        \n",
    "        image_copy[slice_number] = cv2.medianBlur(image_copy[slice_number], 3)\n",
    "        lap = cv2.Laplacian(image_copy[slice_number], cv2.CV_16S)\n",
    "        image_copy[slice_number] = np.clip(image_copy[slice_number].astype(np.int16) - lap, 0, 255).astype(np.uint8)\n",
    "        image_copy[slice_number] = cv2.adaptiveThreshold(image_copy[slice_number], 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, min_l, 0)\n",
    "        \n",
    "        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))\n",
    "        if crown_start <= slice_number <= branch_start+1:\n",
    "            image_copy[slice_number] = cv2.dilate(image_copy[slice_number], kernel, iterations=2)\n",
    "            image_copy[slice_number] = cv2.erode(image_copy[slice_number], kernel, iterations=7)\n",
    "            image_copy[slice_number] = cv2.dilate(image_copy[slice_number], kernel, iterations=3)\n",
    "        else:\n",
    "            image_copy[slice_number] = cv2.erode(image_copy[slice_number], kernel, iterations=5)\n",
    "            image_copy[slice_number] = cv2.dilate(image_copy[slice_number], kernel, iterations=3)\n",
    "    return image_copy\n",
    "\n",
    "def set_window02(image, w_center, w_width, crown_start, branch_start):\n",
    "    image_copy = image.copy()\n",
    "    image_copy = np.clip(image_copy, w_center-int(w_width/2), w_center+int(w_width/2))\n",
    "    image_copy = image_copy.astype(np.float32)\n",
    "    image_copy = ((image_copy - image_copy.min())/(image_copy.max() - image_copy.min())*255).astype(np.uint8)\n",
    "    for slice_number in range(len(image)):\n",
    "        min_l = (int(min(image_copy[slice_number].shape[0]*0.3, image_copy[slice_number].shape[1]*0.3))-1) // 2 * 2 + 1\n",
    "        \n",
    "        image_copy[slice_number] = cv2.GaussianBlur(image_copy[slice_number], (3,3), 1)\n",
    "        image_copy[slice_number] = cv2.adaptiveThreshold(image_copy[slice_number], 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, min_l, 0)\n",
    "        \n",
    "        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))\n",
    "        if crown_start <= slice_number <= branch_start+1:\n",
    "            image_copy[slice_number] = cv2.dilate(image_copy[slice_number], kernel, iterations=2)\n",
    "            image_copy[slice_number] = cv2.erode(image_copy[slice_number], kernel, iterations=7)\n",
    "            image_copy[slice_number] = cv2.dilate(image_copy[slice_number], kernel, iterations=3)\n",
    "        else:\n",
    "            image_copy[slice_number] = cv2.erode(image_copy[slice_number], kernel, iterations=5)\n",
    "            image_copy[slice_number] = cv2.dilate(image_copy[slice_number], kernel, iterations=3)\n",
    "    return image_copy\n",
    "\n",
    "def set_window10(image, w_center, w_width, crown_start, branch_start):\n",
    "    image_copy = image.copy()\n",
    "    image_copy = np.clip(image_copy, w_center-int(w_width/2), w_center+int(w_width/2))\n",
    "    image_copy = image_copy.astype(np.float32)\n",
    "    image_copy = ((image_copy - image_copy.min())/(image_copy.max() - image_copy.min())*255).astype(np.uint8)\n",
    "    for slice_number in range(len(image)):\n",
    "        min_l = (int(min(image_copy[slice_number].shape[0]*0.3, image_copy[slice_number].shape[1]*0.3))-1) // 2 * 2 + 1\n",
    "        \n",
    "        image_copy[slice_number] = cv2.GaussianBlur(image_copy[slice_number], (3,3), 1)\n",
    "        lap = cv2.Laplacian(image_copy[slice_number], cv2.CV_16S)\n",
    "        image_copy[slice_number] = np.clip(image_copy[slice_number].astype(np.int16) - lap, 0, 255).astype(np.uint8)\n",
    "        image_copy[slice_number] = cv2.adaptiveThreshold(image_copy[slice_number], 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, min_l, 0)\n",
    "        \n",
    "        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))\n",
    "        image_copy[slice_number] = cv2.erode(image_copy[slice_number], kernel, iterations=5)\n",
    "        image_copy[slice_number] = cv2.dilate(image_copy[slice_number], kernel, iterations=3)\n",
    "    return image_copy\n",
    "\n",
    "def set_window11(image, w_center, w_width, crown_start, branch_start):\n",
    "    image_copy = image.copy()\n",
    "    image_copy = np.clip(image_copy, w_center-int(w_width/2), w_center+int(w_width/2))\n",
    "    image_copy = image_copy.astype(np.float32)\n",
    "    image_copy = ((image_copy - image_copy.min())/(image_copy.max() - image_copy.min())*255).astype(np.uint8)\n",
    "    for slice_number in range(len(image)):\n",
    "        min_l = (int(min(image_copy[slice_number].shape[0]*0.3, image_copy[slice_number].shape[1]*0.3))-1) // 2 * 2 + 1\n",
    "        \n",
    "        image_copy[slice_number] = cv2.medianBlur(image_copy[slice_number], 3)\n",
    "        lap = cv2.Laplacian(image_copy[slice_number], cv2.CV_16S)\n",
    "        image_copy[slice_number] = np.clip(image_copy[slice_number].astype(np.int16) - lap, 0, 255).astype(np.uint8)\n",
    "        image_copy[slice_number] = cv2.adaptiveThreshold(image_copy[slice_number], 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, min_l, 0)\n",
    "        \n",
    "        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))\n",
    "        image_copy[slice_number] = cv2.erode(image_copy[slice_number], kernel, iterations=5)\n",
    "        image_copy[slice_number] = cv2.dilate(image_copy[slice_number], kernel, iterations=3)\n",
    "    return image_copy\n",
    "\n",
    "def set_window12(image, w_center, w_width, crown_start, branch_start):\n",
    "    image_copy = image.copy()\n",
    "    image_copy = np.clip(image_copy, w_center-int(w_width/2), w_center+int(w_width/2))\n",
    "    image_copy = image_copy.astype(np.float32)\n",
    "    image_copy = ((image_copy - image_copy.min())/(image_copy.max() - image_copy.min())*255).astype(np.uint8)\n",
    "    for slice_number in range(len(image)):\n",
    "        min_l = (int(min(image_copy[slice_number].shape[0]*0.3, image_copy[slice_number].shape[1]*0.3))-1) // 2 * 2 + 1\n",
    "        \n",
    "        image_copy[slice_number] = cv2.GaussianBlur(image_copy[slice_number], (3,3), 1)\n",
    "        image_copy[slice_number] = cv2.adaptiveThreshold(image_copy[slice_number], 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, min_l, 0)\n",
    "        \n",
    "        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))\n",
    "        image_copy[slice_number] = cv2.erode(image_copy[slice_number], kernel, iterations=5)\n",
    "        image_copy[slice_number] = cv2.dilate(image_copy[slice_number], kernel, iterations=3)\n",
    "    return image_copy\n",
    "\n",
    "set_window_list = [set_window00, set_window01, set_window02, set_window10, set_window11, set_window12]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f94789b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_path_and_branch(patient_pixels, set_window_list, img_height, img_width, spine_x, spine_y, crown_start, branch_start, aorta_end, detection_end):\n",
    "    patient_copys = []\n",
    "    for t in range(len(set_window_list)):\n",
    "        patient_copys.append(set_window_list[t](patient_pixels, 300, 600, crown_start, branch_start))\n",
    "            \n",
    "    all_path_list = []\n",
    "    delta = 0\n",
    "    continue_flag = False\n",
    "    while(True):\n",
    "        start_x = max(int(spine_x-img_width*(0.2+delta)), 0)\n",
    "        end_x = min(int(spine_x+img_width*(0.2+delta)), img_width)\n",
    "        start_y = max(int(spine_y-img_height*(0.4+delta)), 0)\n",
    "        end_y = min(int(spine_y+img_height*(0.1+delta)), img_height)\n",
    "        for t in range(len(patient_copys)):\n",
    "            patient_copy = patient_copys[t]\n",
    "            patient_cut = patient_copy[:, start_y:end_y, start_x:end_x]\n",
    "\n",
    "            cut_height, cut_width = patient_cut.shape[1:3]\n",
    "            cut_min_side = min(cut_height, cut_width)\n",
    "            pre_contours, _ = cv2.findContours(patient_cut[0], cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)\n",
    "            pre_circle = list(map(lambda x: cv2.minEnclosingCircle(x), pre_contours))\n",
    "            path_dict = {}\n",
    "            for i in range(1, len(patient_cut)):\n",
    "                cur_contours, _ = cv2.findContours(patient_cut[i], cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)\n",
    "                cur_circle = list(map(lambda x: cv2.minEnclosingCircle(x), cur_contours))\n",
    "                for j in range(len(pre_circle)):\n",
    "                    pre_out_of_range = False\n",
    "                    if pre_circle[j][0][0] < (0.4-delta)*cut_width \\\n",
    "                    or pre_circle[j][0][0] < (0.6-delta)*cut_width and pre_circle[j][0][1] > (0.8+delta)*cut_height:\n",
    "                        pre_out_of_range = True\n",
    "                    candidate_list = []\n",
    "                    for k in range(len(cur_circle)):\n",
    "                        cur_out_of_range = False\n",
    "                        if cur_circle[k][0][0] < (0.4-delta)*cut_width \\\n",
    "                        or cur_circle[k][0][0] < (0.6-delta)*cut_width and cur_circle[k][0][1] > (0.8+delta)*cut_height:\n",
    "                            cur_out_of_range = True\n",
    "                        dis = sqrt((pre_circle[j][0][0]-cur_circle[k][0][0])**2+(pre_circle[j][0][1]-cur_circle[k][0][1])**2)\n",
    "                        max_r, min_r = max(pre_circle[j][1], cur_circle[k][1]), min(pre_circle[j][1], cur_circle[k][1])\n",
    "                        if dis <= max_r:\n",
    "                            #candidate_list.append((k, cv2.contourArea(cur_contours[k])))\n",
    "                            candidate_list.append((k, cur_out_of_range))\n",
    "\n",
    "                    if len(candidate_list) == 0:\n",
    "                        continue\n",
    "                    #candidate_list.sort(key=lambda x:x[1])\n",
    "                    path = path_dict.get((i-1, j), Elem((i-1, j), pre_contours[j], t, pre_out_of_range))\n",
    "                    temp_path_list = [copy.deepcopy(path) for s in range(len(candidate_list))]\n",
    "                    put_back_flag = False\n",
    "                    for s, candidate in enumerate(candidate_list):\n",
    "                        k = candidate[0]\n",
    "                        cur_out_of_range = candidate[1]\n",
    "                        if get_intersection(patient_cut[i], pre_contours[j], cur_contours[k]) < 0.6:\n",
    "                            continue\n",
    "                        temp_path_list[s].append((i, k), cur_contours[k], cur_out_of_range)\n",
    "                        if not (i, k) in path_dict \\\n",
    "                        or path_dict[(i, k)].out_of_range and not temp_path_list[s].out_of_range \\\n",
    "                        or len(path_dict[(i, k)]) < len(temp_path_list[s]) \\\n",
    "                        or path_dict[(i, k)].get_prev_area() < temp_path_list[s].get_prev_area():\n",
    "                            path_dict[(i, k)] = temp_path_list[s]\n",
    "                            put_back_flag = True\n",
    "                    if not put_back_flag:\n",
    "                        path_dict[(i-1, j)] = path\n",
    "\n",
    "                pre_contours = cur_contours\n",
    "                pre_circle = cur_circle\n",
    "\n",
    "            path_list = list(path_dict.values())\n",
    "            all_path_list += path_list\n",
    "\n",
    "        print('all_path_list length:', len(all_path_list))\n",
    "        if len(all_path_list) == 0:\n",
    "            print(\"Can't find aorta!\")\n",
    "            continue_flag = True\n",
    "            break\n",
    "        all_path_list.sort(key=lambda x: len(x), reverse=True)\n",
    "        for i in range(min(3, len(all_path_list))):\n",
    "            print(all_path_list[i].t, ':', len(all_path_list[i]), all_path_list[i].root, '->', all_path_list[i].end, all_path_list[i].out_of_range)\n",
    "        path = None\n",
    "        for tp in all_path_list:\n",
    "            if tp.out_of_range == False:\n",
    "                path = tp\n",
    "                break\n",
    "#         length_factor = 0.6\n",
    "#         if patient_pixels.shape[0] < 300:\n",
    "#             length_factor = 0.45\n",
    "        if path.root[0] > crown_start or path.end[0]+1 < detection_end:#len(path) < length_factor*patient_pixels.shape[0] or all_path_list[0].root[0] > 0.2*patient_pixels.shape[0] \\\n",
    "            delta += 0.05\n",
    "            all_path_list = []\n",
    "            if delta > 0.35:\n",
    "                print(\"Can't find aorta!\")\n",
    "                continue_flag = True\n",
    "                break\n",
    "        else:\n",
    "            break\n",
    "                \n",
    "    if continue_flag:\n",
    "        return continue_flag,\n",
    "    \n",
    "    start, end = path.root[0], path.end[0]+1\n",
    "    path.get_bboxes()\n",
    "    \n",
    "    branch_start_ori = branch_start\n",
    "    branch_start += 1\n",
    "    pre_w = path.bboxes[branch_start-start][2]-path.bboxes[branch_start-start][0]+1\n",
    "    while branch_start > branch_start_ori-1:\n",
    "        cur_w = path.bboxes[branch_start-start-1][2]-path.bboxes[branch_start-start-1][0]+1\n",
    "        if cur_w - pre_w > 0.4 * pre_w:\n",
    "            break\n",
    "        branch_start -= 1\n",
    "        pre_w = cur_w\n",
    "    if branch_start == branch_start_ori-3:\n",
    "        branch_start = branch_start_ori\n",
    "    \n",
    "    get_branch = True\n",
    "    try:\n",
    "        assert branch_start > crown_start\n",
    "        branch_path_list = []\n",
    "        for tmp_path in all_path_list:\n",
    "            if tmp_path.t != path.t or tmp_path.root != path.root or len(tmp_path) < branch_start - start + 1:\n",
    "                continue\n",
    "            if tmp_path.keys[branch_start-start-1] == path.keys[branch_start-start-1] and tmp_path.keys[branch_start-start] != path.keys[branch_start-start]:\n",
    "                branch_path_list.append(tmp_path)\n",
    "        print(f'length of branch_path_list:{len(branch_path_list)}')\n",
    "        branch = branch_path_list[0]\n",
    "        find_branch_flag = False\n",
    "        for tb in branch_path_list:\n",
    "            tb.get_bboxes()\n",
    "            tb_start_w = 0\n",
    "            for tb_i in range(min(5, len(tb)-branch_start+start)):\n",
    "                tb_start_w += tb.bboxes[branch_start-start+tb_i][2]-tb.bboxes[branch_start-start+tb_i][0]+1\n",
    "            tb_start_w /= tb_i+1\n",
    "            tb_start_x = tb_pre_x = (tb.bboxes[branch_start-start][0]+tb.bboxes[branch_start-start][2])//2\n",
    "            tb_pre_y = (tb.bboxes[branch_start-start][1]+tb.bboxes[branch_start-start][3])//2\n",
    "            tb_pre_w = tb.bboxes[branch_start-start][2]-tb.bboxes[branch_start-start][0]+1\n",
    "            tb_i = branch_start+1\n",
    "            break_flag = False\n",
    "            while tb_i < tb.end[0]+1:\n",
    "                tb_cur_w = tb.bboxes[tb_i-start][2]-tb.bboxes[tb_i-start][0]+1\n",
    "                tb_cur_x = (tb.bboxes[tb_i-start][2]+tb.bboxes[tb_i-start][0])/2\n",
    "                tb_cur_y = (tb.bboxes[tb_i-start][3]+tb.bboxes[tb_i-start][1])/2\n",
    "                if tb_cur_y > tb_pre_y and tb_cur_x < tb_pre_x and tb_pre_w - tb_cur_w > tb_cur_w * 0.45:\n",
    "                    break\n",
    "                if tb_i-branch_start > 1 and tb_cur_x > tb_start_x and abs(tb_cur_w - tb_start_w) > 0.4 * tb_start_w:\n",
    "                    break_flag = True\n",
    "                    break\n",
    "                tb_pre_w, tb_pre_x, tb_pre_y = tb_cur_w, tb_cur_x, tb_cur_y\n",
    "                tb_i += 1\n",
    "            if break_flag:\n",
    "                branch = tb\n",
    "                branch_end = tb_i\n",
    "                find_branch_flag = True\n",
    "                break\n",
    "        \n",
    "        if not find_branch_flag:\n",
    "            branch.get_bboxes()\n",
    "            branch_end = branch_start+1\n",
    "            branch_start_w = 0\n",
    "            for b_i in range(min(5, len(branch)-branch_start+start)):\n",
    "                branch_start_w += branch.bboxes[branch_start-start+b_i][2]-branch.bboxes[branch_start-start+b_i][0]+1\n",
    "            branch_start_w /= b_i+1\n",
    "            branch_start_x = pre_x = (branch.bboxes[branch_start-start][0]+branch.bboxes[branch_start-start][2])//2\n",
    "            pre_y = (branch.bboxes[branch_start-start][1]+branch.bboxes[branch_start-start][3])//2\n",
    "            pre_w = branch.bboxes[branch_start-start][2]-branch.bboxes[branch_start-start][0]+1\n",
    "            while branch_end < branch.end[0]+1:\n",
    "                cur_w = branch.bboxes[branch_end-start][2]-branch.bboxes[branch_end-start][0]+1\n",
    "                cur_x = (branch.bboxes[branch_end-start][2]+branch.bboxes[branch_end-start][0])/2\n",
    "                cur_y = (branch.bboxes[branch_end-start][3]+branch.bboxes[branch_end-start][1])/2\n",
    "                if branch_end-branch_start > 1 and (cur_x > branch_start_x and abs(cur_w - branch_start_w) > 0.4 * branch_start_w \\\n",
    "                or cur_y > pre_y and cur_x < pre_x and pre_w - cur_w > cur_w * 0.45):\n",
    "                    break\n",
    "                branch_end += 1\n",
    "                pre_w, pre_x, pre_y = cur_w, cur_x, cur_y\n",
    "        assert branch_end < end\n",
    "    except:\n",
    "        traceback.print_exc()\n",
    "        branch = all_path_list[-1]\n",
    "        get_branch = False\n",
    "        branch_start = branch_end = -1\n",
    "        \n",
    "    return continue_flag, path, branch, start, end, branch_start, branch_end, get_branch, start_x, start_y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a09f5303",
   "metadata": {},
   "outputs": [],
   "source": [
    "input_folder_list = []\n",
    "root_path = '/nfs3-p1/zsxm/dataset/aorta/'\n",
    "for path in os.listdir(root_path):\n",
    "    if not os.path.isdir(os.path.join(root_path, path)):\n",
    "        continue\n",
    "    if not os.path.exists(os.path.join(root_path, path, '2/')):\n",
    "        continue\n",
    "    else:\n",
    "        input_folder_list.append(os.path.join(root_path, path, '2/'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f94edc52",
   "metadata": {},
   "outputs": [],
   "source": [
    "input_folder_list = [\n",
    "                     '/nfs3-p1/zsxm/dataset/aorta_CTA/chenxiu/img/',\n",
    "                     '/nfs3-p1/zsxm/dataset/aorta_CTA/dingxing/img/',\n",
    "                     '/nfs3-p1/zsxm/dataset/aorta_CTA/huanggaoqiang/img/',\n",
    "                     '/nfs3-p1/zsxm/dataset/aorta_CTA/louhangqing/img/',\n",
    "                     '/nfs3-p1/zsxm/dataset/aorta_CTA/wangxiaowei/yes/',\n",
    "                     '/nfs3-p1/zsxm/dataset/aorta_CTA/xiangxijiao/img/',\n",
    "                     '/nfs3-p1/zsxm/dataset/aorta_CTA/yangxiangqin/yes/',\n",
    "                     '/nfs3-p1/zsxm/dataset/aorta_CTA/zhaoqifeng/img/',\n",
    "                     '/nfs3-p1/zsxm/dataset/aorta_CTA/zhujingchun/img/',\n",
    "                     '/nfs3-p1/zsxm/dataset/aorta_CTA/zuochun/img/',\n",
    "                     '/nfs3-p1/zsxm/dataset/aortic_dissection/CTA1(+)/+C/',\n",
    "                     '/nfs3-p1/zsxm/dataset/aortic_dissection/CTA2(+)/+C/',\n",
    "                     '/nfs3-p1/zsxm/dataset/aortic_dissection/CTA2(-)/+c/',\n",
    "                     '/nfs3-p1/zsxm/dataset/aortic_dissection/CTA3(+)/+C/',\n",
    "                    ]\n",
    "# input_folder_list = [\n",
    "#     \"/nfs3-p1/zsxm/dataset/aorta/chuguangzhou-Im46-111/2/\",\n",
    "#     \"/nfs3-p1/zsxm/dataset/aorta/modehe-Im34-89/2/\",\n",
    "#     \"/nfs3-p1/zsxm/dataset/aorta/yuliyao-Im25-31/2/\",\n",
    "#     \"/nfs3-p1/zsxm/dataset/aorta/jiangquanfeng-Im 50-107/2/\",\n",
    "#     \"/nfs3-p1/zsxm/dataset/aorta/shuzhiming-Im28-80/2/\",\n",
    "#     \"/nfs3-p1/zsxm/dataset/aorta_CTA/huanggaoqiang/img/\",\n",
    "#     \"/nfs3-p1/zsxm/dataset/aorta_CTA/xiangxijiao/img/\",\n",
    "#     '/nfs3-p1/zsxm/dataset/aorta/shenjiaren-Im35-136/2/',\n",
    "#     '/nfs3-p1/zsxm/dataset/aorta/wanggaosheng-Im24-57/2/',\n",
    "#     '/nfs3-p1/zsxm/dataset/aorta/wangzhongwen-Im36-80/2/',\n",
    "#     '/nfs3-p1/zsxm/dataset/aorta/xujinyang-Im15-30/2/',\n",
    "#     '/nfs3-p1/zsxm/dataset/aorta/zhangxinliang-Im16-24/2/',\n",
    "#     '/nfs3-p1/zsxm/dataset/aorta/fengyonghua-Im21-89/2/',\n",
    "# ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1adb386a",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/chenxiu/img/save_processed\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/chenxiu/img/save_raw\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/chenxiu/img/images\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/chenxiu/img/labels\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/dingxing/img/save_processed\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/dingxing/img/save_raw\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/dingxing/img/images\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/dingxing/img/labels\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/huanggaoqiang/img/save_processed\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/huanggaoqiang/img/save_raw\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/huanggaoqiang/img/images\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/huanggaoqiang/img/labels\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/louhangqing/img/save_processed\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/louhangqing/img/save_raw\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/louhangqing/img/images\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/louhangqing/img/labels\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/wangxiaowei/yes/save_processed\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/wangxiaowei/yes/save_raw\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/wangxiaowei/yes/images\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/wangxiaowei/yes/labels\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/xiangxijiao/img/save_processed\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/xiangxijiao/img/save_raw\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/xiangxijiao/img/images\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/xiangxijiao/img/labels\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/yangxiangqin/yes/save_processed\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/yangxiangqin/yes/save_raw\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/yangxiangqin/yes/images\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/yangxiangqin/yes/labels\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/zhaoqifeng/img/save_processed\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/zhaoqifeng/img/save_raw\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/zhaoqifeng/img/images\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/zhaoqifeng/img/labels\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/zhujingchun/img/save_processed\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/zhujingchun/img/save_raw\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/zhujingchun/img/images\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/zhujingchun/img/labels\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/zuochun/img/save_processed\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/zuochun/img/save_raw\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/zuochun/img/images\n",
      "remove /nfs3-p1/zsxm/dataset/aorta_CTA/zuochun/img/labels\n",
      "remove /nfs3-p1/zsxm/dataset/aortic_dissection/CTA1(+)/+C/save_processed\n",
      "remove /nfs3-p1/zsxm/dataset/aortic_dissection/CTA1(+)/+C/save_raw\n",
      "remove /nfs3-p1/zsxm/dataset/aortic_dissection/CTA1(+)/+C/images\n",
      "remove /nfs3-p1/zsxm/dataset/aortic_dissection/CTA1(+)/+C/labels\n",
      "remove /nfs3-p1/zsxm/dataset/aortic_dissection/CTA2(+)/+C/save_processed\n",
      "remove /nfs3-p1/zsxm/dataset/aortic_dissection/CTA2(+)/+C/save_raw\n",
      "remove /nfs3-p1/zsxm/dataset/aortic_dissection/CTA2(+)/+C/images\n",
      "remove /nfs3-p1/zsxm/dataset/aortic_dissection/CTA2(+)/+C/labels\n",
      "remove /nfs3-p1/zsxm/dataset/aortic_dissection/CTA2(-)/+c/save_processed\n",
      "remove /nfs3-p1/zsxm/dataset/aortic_dissection/CTA2(-)/+c/save_raw\n",
      "remove /nfs3-p1/zsxm/dataset/aortic_dissection/CTA2(-)/+c/images\n",
      "remove /nfs3-p1/zsxm/dataset/aortic_dissection/CTA2(-)/+c/labels\n",
      "remove /nfs3-p1/zsxm/dataset/aortic_dissection/CTA3(+)/+C/save_processed\n",
      "remove /nfs3-p1/zsxm/dataset/aortic_dissection/CTA3(+)/+C/save_raw\n",
      "remove /nfs3-p1/zsxm/dataset/aortic_dissection/CTA3(+)/+C/images\n",
      "remove /nfs3-p1/zsxm/dataset/aortic_dissection/CTA3(+)/+C/labels\n"
     ]
    }
   ],
   "source": [
    "for p in input_folder_list:\n",
    "    if os.path.exists(os.path.join(p, 'save')):\n",
    "        shutil.rmtree(os.path.join(p, 'save'))\n",
    "        print(f\"remove {os.path.join(p, 'save')}\")\n",
    "    if os.path.exists(os.path.join(p, 'save_post')):\n",
    "        shutil.rmtree(os.path.join(p, 'save_post'))\n",
    "        print(f\"remove {os.path.join(p, 'save_post')}\")\n",
    "    if os.path.exists(os.path.join(p, 'save_processed')):\n",
    "        shutil.rmtree(os.path.join(p, 'save_processed'))\n",
    "        print(f\"remove {os.path.join(p, 'save_processed')}\")\n",
    "    if os.path.exists(os.path.join(p, 'save_raw')):\n",
    "        shutil.rmtree(os.path.join(p, 'save_raw'))\n",
    "        print(f\"remove {os.path.join(p, 'save_raw')}\")\n",
    "    if os.path.exists(os.path.join(p, 'save_raw_blur')):\n",
    "        shutil.rmtree(os.path.join(p, 'save_raw_blur'))\n",
    "        print(f\"remove {os.path.join(p, 'save_raw_blur')}\")\n",
    "    if os.path.exists(os.path.join(p, 'images')):\n",
    "        shutil.rmtree(os.path.join(p, 'images'))\n",
    "        print(f\"remove {os.path.join(p, 'images')}\")\n",
    "    if os.path.exists(os.path.join(p, 'labels')):\n",
    "        shutil.rmtree(os.path.join(p, 'labels'))\n",
    "        print(f\"remove {os.path.join(p, 'labels')}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "903b1903",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "******Processing: /nfs3-p1/zsxm/dataset/aorta_CTA/chenxiu/img/  ******\n",
      "sequence length: 737, height: 512, width: 512\n",
      "find spine area: 783.5\n",
      "times:0.3\n",
      "all_path_list length: 33028\n",
      "2 : 737 (0, 15) -> (736, 10) False\n",
      "2 : 736 (0, 15) -> (735, 9) False\n",
      "2 : 735 (0, 15) -> (734, 10) False\n",
      "length of branch_path_list:500\n",
      "crown_start:53, aorta_end:566, detection_end:575, branch_start:82, branch_end:193\n",
      "******Processing: /nfs3-p1/zsxm/dataset/aorta_CTA/dingxing/img/  ******\n",
      "sequence length: 521, height: 512, width: 512\n",
      "find spine area: 936.0\n",
      "times:0.49999999999999994\n",
      "all_path_list length: 22318\n",
      "0 : 465 (56, 12) -> (520, 15) False\n",
      "0 : 464 (56, 12) -> (519, 16) False\n",
      "0 : 463 (56, 12) -> (518, 13) False\n",
      "length of branch_path_list:350\n",
      "crown_start:141, aorta_end:520, detection_end:520, branch_start:167, branch_end:241\n",
      "******Processing: /nfs3-p1/zsxm/dataset/aorta_CTA/huanggaoqiang/img/  ******\n",
      "sequence length: 532, height: 512, width: 512\n",
      "find spine area: 649.5\n",
      "times:0.5499999999999999\n",
      "all_path_list length: 17440\n",
      "1 : 354 (178, 3) -> (531, 10) False\n",
      "1 : 354 (178, 3) -> (531, 11) False\n",
      "1 : 353 (178, 3) -> (530, 5) False\n",
      "all_path_list length: 20162\n",
      "1 : 354 (178, 7) -> (531, 11) False\n",
      "1 : 354 (178, 7) -> (531, 12) False\n",
      "1 : 353 (178, 7) -> (530, 6) False\n",
      "all_path_list length: 23134\n",
      "0 : 496 (36, 12) -> (531, 6) False\n",
      "0 : 496 (36, 12) -> (531, 7) False\n",
      "0 : 495 (36, 12) -> (530, 5) False\n",
      "length of branch_path_list:60\n",
      "crown_start:81, aorta_end:510, detection_end:521, branch_start:106, branch_end:140\n",
      "******Processing: /nfs3-p1/zsxm/dataset/aorta_CTA/louhangqing/img/  ******\n",
      "sequence length: 984, height: 512, width: 512\n",
      "find spine area: 680.5\n",
      "times:0.5499999999999999\n",
      "all_path_list length: 40729\n",
      "2 : 579 (405, 21) -> (983, 13) True\n",
      "2 : 578 (405, 21) -> (982, 13) True\n",
      "2 : 577 (405, 21) -> (981, 12) True\n",
      "all_path_list length: 49101\n",
      "0 : 867 (46, 8) -> (912, 13) False\n",
      "0 : 866 (46, 8) -> (911, 12) False\n",
      "0 : 865 (46, 8) -> (910, 16) False\n",
      "length of branch_path_list:2\n",
      "主动脉弓部分出现了尺寸大于整张图一半的边界框，重做！\n",
      "all_path_list length: 40360\n",
      "2 : 579 (405, 21) -> (983, 13) True\n",
      "2 : 578 (405, 21) -> (982, 13) True\n",
      "2 : 577 (405, 21) -> (981, 12) True\n",
      "all_path_list length: 48630\n",
      "2 : 939 (45, 15) -> (983, 21) True\n",
      "2 : 938 (45, 15) -> (982, 20) True\n",
      "2 : 937 (45, 15) -> (981, 17) True\n",
      "length of branch_path_list:721\n",
      "crown_start:103, aorta_end:615, detection_end:632, branch_start:128, branch_end:148\n",
      "******Processing: /nfs3-p1/zsxm/dataset/aorta_CTA/wangxiaowei/yes/  ******\n",
      "sequence length: 841, height: 512, width: 512\n",
      "find spine area: 2160.0\n",
      "times:0.49999999999999994\n",
      "all_path_list length: 41122\n",
      "0 : 791 (50, 11) -> (840, 7) True\n",
      "0 : 791 (50, 11) -> (840, 8) False\n",
      "0 : 790 (50, 11) -> (839, 5) True\n",
      "length of branch_path_list:85\n",
      "crown_start:110, aorta_end:648, detection_end:665, branch_start:137, branch_end:149\n",
      "******Processing: /nfs3-p1/zsxm/dataset/aorta_CTA/xiangxijiao/img/  ******\n",
      "sequence length: 719, height: 512, width: 512\n",
      "find spine area: 803.0\n",
      "times:0.35\n",
      "all_path_list length: 22526\n",
      "2 : 555 (0, 11) -> (554, 17) True\n",
      "2 : 554 (0, 11) -> (553, 14) True\n",
      "2 : 553 (0, 11) -> (552, 11) True\n",
      "length of branch_path_list:534\n",
      "crown_start:52, aorta_end:445, detection_end:455, branch_start:67, branch_end:70\n",
      "******Processing: /nfs3-p1/zsxm/dataset/aorta_CTA/yangxiangqin/yes/  ******\n",
      "sequence length: 808, height: 512, width: 512\n",
      "find spine area: 2117.5\n",
      "times:0.15000000000000002\n",
      "all_path_list length: 34909\n",
      "0 : 808 (0, 3) -> (807, 5) True\n",
      "0 : 807 (0, 3) -> (806, 10) True\n",
      "0 : 806 (0, 3) -> (805, 7) True\n",
      "length of branch_path_list:297\n",
      "crown_start:43, aorta_end:483, detection_end:502, branch_start:63, branch_end:169\n",
      "******Processing: /nfs3-p1/zsxm/dataset/aorta_CTA/zhaoqifeng/img/  ******\n",
      "sequence length: 651, height: 512, width: 512\n",
      "find spine area: 600.0\n",
      "times:0.5499999999999999\n",
      "all_path_list length: 19488\n",
      "2 : 566 (85, 8) -> (650, 6) False\n",
      "2 : 566 (85, 8) -> (650, 7) True\n",
      "2 : 565 (85, 8) -> (649, 8) False\n",
      "length of branch_path_list:636\n",
      "crown_start:86, aorta_end:502, detection_end:515, branch_start:116, branch_end:140\n",
      "******Processing: /nfs3-p1/zsxm/dataset/aorta_CTA/zhujingchun/img/  ******\n",
      "sequence length: 807, height: 512, width: 512\n",
      "find spine area: 676.5\n",
      "times:0.35\n",
      "all_path_list length: 40137\n",
      "0 : 807 (0, 10) -> (806, 11) False\n",
      "2 : 807 (0, 15) -> (806, 8) True\n",
      "2 : 807 (0, 15) -> (806, 9) True\n",
      "length of branch_path_list:383\n",
      "crown_start:82, aorta_end:532, detection_end:593, branch_start:115, branch_end:151\n",
      "******Processing: /nfs3-p1/zsxm/dataset/aorta_CTA/zuochun/img/  ******\n",
      "sequence length: 961, height: 512, width: 512\n",
      "find spine area: 1128.5\n",
      "times:0.25\n",
      "all_path_list length: 35273\n",
      "0 : 579 (42, 9) -> (620, 5) True\n",
      "0 : 578 (42, 9) -> (619, 8) True\n",
      "0 : 577 (42, 9) -> (618, 7) True\n",
      "all_path_list length: 40507\n",
      "2 : 704 (40, 18) -> (743, 11) True\n",
      "2 : 703 (40, 18) -> (742, 10) True\n",
      "2 : 703 (40, 18) -> (742, 11) True\n",
      "length of branch_path_list:622\n",
      "crown_start:99, aorta_end:607, detection_end:633, branch_start:115, branch_end:183\n",
      "******Processing: /nfs3-p1/zsxm/dataset/aortic_dissection/CTA1(+)/+C/  ******\n",
      "sequence length: 513, height: 512, width: 512\n",
      "find spine area: 672.0\n",
      "times:0.49999999999999994\n",
      "all_path_list length: 16963\n",
      "2 : 491 (0, 14) -> (490, 9) True\n",
      "0 : 490 (0, 7) -> (489, 13) True\n",
      "2 : 490 (0, 14) -> (489, 7) True\n",
      "length of branch_path_list:113\n",
      "crown_start:61, aorta_end:266, detection_end:357, branch_start:74, branch_end:140\n",
      "******Processing: /nfs3-p1/zsxm/dataset/aortic_dissection/CTA2(+)/+C/  ******\n",
      "sequence length: 642, height: 512, width: 512\n",
      "find spine area: 795.0\n",
      "times:0.44999999999999996\n",
      "all_path_list length: 30795\n",
      "0 : 597 (45, 11) -> (641, 6) False\n",
      "0 : 596 (45, 11) -> (640, 11) False\n",
      "0 : 595 (45, 11) -> (639, 12) False\n",
      "length of branch_path_list:173\n",
      "crown_start:116, aorta_end:467, detection_end:641, branch_start:153, branch_end:249\n",
      "******Processing: /nfs3-p1/zsxm/dataset/aortic_dissection/CTA2(-)/+c/  ******\n",
      "sequence length: 807, height: 512, width: 512\n",
      "find spine area: 676.5\n",
      "times:0.35\n",
      "all_path_list length: 40172\n",
      "0 : 807 (0, 10) -> (806, 11) False\n",
      "2 : 807 (0, 15) -> (806, 8) True\n",
      "2 : 807 (0, 15) -> (806, 9) True\n",
      "length of branch_path_list:383\n",
      "crown_start:80, aorta_end:573, detection_end:593, branch_start:115, branch_end:151\n",
      "******Processing: /nfs3-p1/zsxm/dataset/aortic_dissection/CTA3(+)/+C/  ******\n",
      "sequence length: 707, height: 512, width: 512\n",
      "find spine area: 621.5\n",
      "times:0.5499999999999999\n",
      "all_path_list length: 17468\n",
      "2 : 707 (0, 13) -> (706, 9) False\n",
      "2 : 707 (0, 13) -> (706, 12) True\n",
      "2 : 706 (0, 13) -> (705, 6) False\n",
      "length of branch_path_list:78\n",
      "crown_start:31, aorta_end:495, detection_end:508, branch_start:49, branch_end:108\n"
     ]
    }
   ],
   "source": [
    "for p in range(len(input_folder_list)):\n",
    "    print(f'******Processing: {input_folder_list[p]}  ******')\n",
    "    patient = load_scan(input_folder_list[p])\n",
    "    patient_pixels = get_pixels_hu(patient)\n",
    "    img_height, img_width = patient_pixels.shape[1:3]\n",
    "    print(f'sequence length: {patient_pixels.shape[0]}, height: {img_height}, width: {img_width}')\n",
    "    spine_x, spine_y = 0, 0\n",
    "    \n",
    "    patient_sum = np.sum(patient_pixels, axis=0)\n",
    "    patient_sum = patient_sum.astype(np.float32)\n",
    "    max_val = patient_sum.max()\n",
    "    times = 0.6\n",
    "    while times > 0:\n",
    "        thr, patient_thr = cv2.threshold(patient_sum, max_val*times, 255, cv2.THRESH_BINARY)\n",
    "        patient_thr = patient_thr.astype(np.uint8)\n",
    "        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))\n",
    "        patient_thr = cv2.erode(patient_thr, kernel, iterations=2)\n",
    "        sum_contours, _ = cv2.findContours(patient_thr, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)\n",
    "        sum_contours_list = [(cv2.contourArea(contour), contour) for contour in sum_contours]\n",
    "        sum_contours_list.sort(key=lambda x: x[0], reverse=True)\n",
    "        if len(sum_contours_list) == 0 \\\n",
    "        or len(sum_contours_list) > 1 and sum_contours_list[0][0] < 2 * sum_contours_list[1][0] \\\n",
    "        or sum_contours_list[0][0] < img_height*img_width*0.002:\n",
    "            times -= 0.05\n",
    "            continue\n",
    "        find_flag = False\n",
    "        for area, contour in sum_contours_list:\n",
    "            print(f'find spine area: {area}')\n",
    "            x, y, w, h = cv2.boundingRect(contour)\n",
    "            cx, cy = x+w//2, y+h//2\n",
    "            if img_height/2 < cy < img_height*0.75 and img_width*0.375 < cx < img_width*0.625:\n",
    "                spine_x, spine_y = cx, cy\n",
    "                find_flag = True\n",
    "                break\n",
    "        if find_flag:\n",
    "            print(f'times:{times}')\n",
    "            break\n",
    "        times -= 0.05\n",
    "\n",
    "    if spine_x == 0 or spine_y == 0:\n",
    "        #raise Exception(f'spine_x:{spine_x}, spine_y:{spine_y}')\n",
    "        print(f\"spine_x:{spine_x}, spine_y:{spine_y}, can't find spine\")\n",
    "        continue\n",
    "        \n",
    "    with open(os.path.join(input_folder_list[p], 'branch.txt'), 'r') as f:\n",
    "        txt_result = f.readlines()\n",
    "        crown_start = int(txt_result[0])\n",
    "        ori_branch_start = int(txt_result[1])\n",
    "        aorta_end = int(txt_result[2])\n",
    "        detection_end = int(txt_result[3])\n",
    "    \n",
    "    result = get_path_and_branch(patient_pixels, set_window_list[0:3], img_height, img_width, spine_x, spine_y, crown_start, ori_branch_start, aorta_end, detection_end)\n",
    "    if result[0] == True:\n",
    "        continue\n",
    "    path, branch, start, end, branch_start, branch_end, get_branch, start_x, start_y = result[1:]\n",
    "    redo_flag = False\n",
    "    for i in range(crown_start, branch_start+2):\n",
    "        bw = path.bboxes[i-start][2] - path.bboxes[i-start][0] + 1\n",
    "        if bw > 0.5 * img_width:\n",
    "            redo_flag = True\n",
    "            break\n",
    "    if redo_flag:\n",
    "        print('主动脉弓部分出现了尺寸大于整张图一半的边界框，重做！')\n",
    "        result = get_path_and_branch(patient_pixels, set_window_list[3:6], img_height, img_width, spine_x, spine_y, crown_start, ori_branch_start, aorta_end, detection_end)\n",
    "        if result[0] == True:\n",
    "            continue\n",
    "        path, branch, start, end, branch_start, branch_end, get_branch, start_x, start_y = result[1:]\n",
    "    \n",
    "    print(f'crown_start:{crown_start}, aorta_end:{aorta_end}, detection_end:{detection_end}, branch_start:{branch_start}, branch_end:{branch_end}')\n",
    "    \n",
    "    patient = load_scan(input_folder_list[p])\n",
    "    draw_bbox(patient, path, start_x, start_y, branch, branch_start, branch_end, crown_start, aorta_end, os.path.join(input_folder_list[p], 'save_raw'))\n",
    "    \n",
    "    path.blur_bboxes(kind=0)\n",
    "    if get_branch:\n",
    "        branch.blur_bboxes(kind=0)\n",
    "#     patient = load_scan(input_folder_list[p])\n",
    "#     draw_bbox(patient, path, start_x, start_y, branch, branch_start, branch_end, crown_start, aorta_end, os.path.join(input_folder_list[p], 'save_raw_blur'))\n",
    "    \n",
    "    path.post_process(patient_pixels, start_x, start_y)\n",
    "    if get_branch:\n",
    "        branch.post_process(patient_pixels, start_x, start_y)\n",
    "#     patient = load_scan(input_folder_list[p])\n",
    "#     draw_post_bbox(patient, path, branch, branch_start, branch_end, crown_start, aorta_end, os.path.join(input_folder_list[p], 'save_post'))\n",
    "    \n",
    "    path.blur_bboxes(kind=1)\n",
    "    if get_branch:\n",
    "        branch.blur_bboxes(kind=1)\n",
    "    patient = load_scan(input_folder_list[p])\n",
    "    draw_post_bbox(patient, path, branch, branch_start, branch_end, crown_start, aorta_end, os.path.join(input_folder_list[p], 'save_processed'))\n",
    "    \n",
    "    patient = load_scan(input_folder_list[p])\n",
    "    output_image_and_label(patient, path, branch, branch_start, branch_end, crown_start, detection_end, input_folder_list[p])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c0384c0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c5fd4f75",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Copy /nfs3-p1/zsxm/dataset/aorta_CTA/chenxiu/img/ To /nfs3-p1/zsxm/dataset/aorta_img_label2/chenxiu\n",
      "Copy /nfs3-p1/zsxm/dataset/aorta_CTA/dingxing/img/ To /nfs3-p1/zsxm/dataset/aorta_img_label2/dingxing\n",
      "Copy /nfs3-p1/zsxm/dataset/aorta_CTA/huanggaoqiang/img/ To /nfs3-p1/zsxm/dataset/aorta_img_label2/huanggaoqiang\n",
      "Copy /nfs3-p1/zsxm/dataset/aorta_CTA/louhangqing/img/ To /nfs3-p1/zsxm/dataset/aorta_img_label2/louhangqing\n",
      "Copy /nfs3-p1/zsxm/dataset/aorta_CTA/wangxiaowei/yes/ To /nfs3-p1/zsxm/dataset/aorta_img_label2/wangxiaowei\n",
      "Copy /nfs3-p1/zsxm/dataset/aorta_CTA/xiangxijiao/img/ To /nfs3-p1/zsxm/dataset/aorta_img_label2/xiangxijiao\n",
      "Copy /nfs3-p1/zsxm/dataset/aorta_CTA/yangxiangqin/yes/ To /nfs3-p1/zsxm/dataset/aorta_img_label2/yangxiangqin\n",
      "Copy /nfs3-p1/zsxm/dataset/aorta_CTA/zhaoqifeng/img/ To /nfs3-p1/zsxm/dataset/aorta_img_label2/zhaoqifeng\n",
      "Copy /nfs3-p1/zsxm/dataset/aorta_CTA/zhujingchun/img/ To /nfs3-p1/zsxm/dataset/aorta_img_label2/zhujingchun\n",
      "Copy /nfs3-p1/zsxm/dataset/aorta_CTA/zuochun/img/ To /nfs3-p1/zsxm/dataset/aorta_img_label2/zuochun\n",
      "Copy /nfs3-p1/zsxm/dataset/aortic_dissection/CTA1(+)/+C/ To /nfs3-p1/zsxm/dataset/aorta_img_label2/CTA1(+)\n",
      "Copy /nfs3-p1/zsxm/dataset/aortic_dissection/CTA2(+)/+C/ To /nfs3-p1/zsxm/dataset/aorta_img_label2/CTA2(+)\n",
      "Copy /nfs3-p1/zsxm/dataset/aortic_dissection/CTA2(-)/+c/ To /nfs3-p1/zsxm/dataset/aorta_img_label2/CTA2(-)\n",
      "Copy /nfs3-p1/zsxm/dataset/aortic_dissection/CTA3(+)/+C/ To /nfs3-p1/zsxm/dataset/aorta_img_label2/CTA3(+)\n"
     ]
    }
   ],
   "source": [
    "dst_path = '/nfs3-p1/zsxm/dataset/aorta_img_label2/'\n",
    "if not os.path.exists(dst_path):\n",
    "    os.mkdir(dst_path)\n",
    "for p in input_folder_list:\n",
    "    sl = p.split('/')\n",
    "    name = sl[-2] if sl[-1] != '' else sl[-3]\n",
    "    if os.path.exists(os.path.join(dst_path, name)):\n",
    "        print(f\"remove {os.path.join(dst_path, name)}\")\n",
    "        shutil.rmtree(os.path.join(dst_path, name))\n",
    "    os.mkdir(os.path.join(dst_path, name))\n",
    "        \n",
    "    print(f\"Copy {os.path.join(p)} To {os.path.join(dst_path, name)}\")\n",
    "    try:\n",
    "        shutil.copytree(os.path.join(p, 'images'), os.path.join(dst_path, name, 'images'))\n",
    "        shutil.copytree(os.path.join(p, 'labels'), os.path.join(dst_path, name, 'labels'))\n",
    "    except:\n",
    "        traceback.print_exc()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "270265f7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c97f2ca2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0092aa00",
   "metadata": {},
   "outputs": [],
   "source": [
    "# for p in range(len(input_folder_list)):\n",
    "#     print(f'******Processing: {input_folder_list[p]}  ******')\n",
    "#     patient = load_scan(input_folder_list[p])\n",
    "#     patient_pixels = get_pixels_hu(patient)\n",
    "#     img_height, img_width = patient_pixels.shape[1:3]\n",
    "#     print(f'sequence length: {patient_pixels.shape[0]}, height: {img_height}, width: {img_width}')\n",
    "#     spine_x, spine_y = 0, 0\n",
    "    \n",
    "#     patient_sum = np.sum(patient_pixels, axis=0)\n",
    "#     patient_sum = patient_sum.astype(np.float32)\n",
    "#     max_val = patient_sum.max()\n",
    "#     times = 0.6\n",
    "#     while times > 0:\n",
    "#         thr, patient_thr = cv2.threshold(patient_sum, max_val*times, 255, cv2.THRESH_BINARY)\n",
    "#         patient_thr = patient_thr.astype(np.uint8)\n",
    "#         kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))\n",
    "#         patient_thr = cv2.erode(patient_thr, kernel, iterations=2)\n",
    "#         sum_contours, _ = cv2.findContours(patient_thr, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)\n",
    "#         sum_contours_list = [(cv2.contourArea(contour), contour) for contour in sum_contours]\n",
    "#         sum_contours_list.sort(key=lambda x: x[0], reverse=True)\n",
    "#         if len(sum_contours_list) == 0 \\\n",
    "#         or len(sum_contours_list) > 1 and sum_contours_list[0][0] < 2 * sum_contours_list[1][0] \\\n",
    "#         or sum_contours_list[0][0] < img_height*img_width*0.002:\n",
    "#             times -= 0.05\n",
    "#             continue\n",
    "#         find_flag = False\n",
    "#         for area, contour in sum_contours_list:\n",
    "#             print(f'find spine area: {area}')\n",
    "#             x, y, w, h = cv2.boundingRect(contour)\n",
    "#             cx, cy = x+w//2, y+h//2\n",
    "#             if img_height/2 < cy < img_height*0.75 and img_width*0.375 < cx < img_width*0.625:\n",
    "#                 spine_x, spine_y = cx, cy\n",
    "#                 find_flag = True\n",
    "#                 break\n",
    "#         if find_flag:\n",
    "#             print(f'times:{times}')\n",
    "#             break\n",
    "#         times -= 0.05\n",
    "\n",
    "#     if spine_x == 0 or spine_y == 0:\n",
    "#         #raise Exception(f'spine_x:{spine_x}, spine_y:{spine_y}')\n",
    "#         print(f\"spine_x:{spine_x}, spine_y:{spine_y}, can't find spine\")\n",
    "#         continue\n",
    "        \n",
    "#     with open(os.path.join(input_folder_list[p], 'branch.txt'), 'r') as f:\n",
    "#         txt_result = f.readlines()\n",
    "#         crown_start = int(txt_result[0])\n",
    "#         branch_start = int(txt_result[1])\n",
    "#         aorta_end = int(txt_result[2])\n",
    "#         detection_end = int(txt_result[3])\n",
    "       \n",
    "#     patient_copys = []\n",
    "#     for t in range(len(set_window_list)):\n",
    "#         patient_copys.append(set_window_list[t](patient_pixels, 300, 600, crown_start, branch_start))\n",
    "            \n",
    "#     all_path_list = []\n",
    "#     delta = 0\n",
    "#     continue_flag = False\n",
    "#     while(True):\n",
    "#         start_x = max(int(spine_x-img_width*(0.2+delta)), 0)\n",
    "#         end_x = min(int(spine_x+img_width*(0.2+delta)), img_width)\n",
    "#         start_y = max(int(spine_y-img_height*(0.4+delta)), 0)\n",
    "#         end_y = min(int(spine_y+img_height*(0.1+delta)), img_height)\n",
    "#         for t in range(len(patient_copys)):\n",
    "#             patient_copy = patient_copys[t]\n",
    "#             patient_cut = patient_copy[:, start_y:end_y, start_x:end_x]\n",
    "\n",
    "#             cut_height, cut_width = patient_cut.shape[1:3]\n",
    "#             cut_min_side = min(cut_height, cut_width)\n",
    "#             pre_contours, _ = cv2.findContours(patient_cut[0], cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)\n",
    "#             pre_circle = list(map(lambda x: cv2.minEnclosingCircle(x), pre_contours))\n",
    "#             path_dict = {}\n",
    "#             for i in range(1, len(patient_cut)):\n",
    "#                 cur_contours, _ = cv2.findContours(patient_cut[i], cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)\n",
    "#                 cur_circle = list(map(lambda x: cv2.minEnclosingCircle(x), cur_contours))\n",
    "#                 for j in range(len(pre_circle)):\n",
    "#                     pre_out_of_range = False\n",
    "#                     if pre_circle[j][0][0] < (0.4-delta)*cut_width \\\n",
    "#                     or pre_circle[j][0][0] < (0.6-delta)*cut_width and pre_circle[j][0][1] > (0.8+delta)*cut_height:\n",
    "#                         pre_out_of_range = True\n",
    "#                     candidate_list = []\n",
    "#                     for k in range(len(cur_circle)):\n",
    "#                         cur_out_of_range = False\n",
    "#                         if cur_circle[k][0][0] < (0.4-delta)*cut_width \\\n",
    "#                         or cur_circle[k][0][0] < (0.6-delta)*cut_width and cur_circle[k][0][1] > (0.8+delta)*cut_height:\n",
    "#                             cur_out_of_range = True\n",
    "#                         dis = sqrt((pre_circle[j][0][0]-cur_circle[k][0][0])**2+(pre_circle[j][0][1]-cur_circle[k][0][1])**2)\n",
    "#                         max_r, min_r = max(pre_circle[j][1], cur_circle[k][1]), min(pre_circle[j][1], cur_circle[k][1])\n",
    "#                         if dis <= max_r:\n",
    "#                             #candidate_list.append((k, cv2.contourArea(cur_contours[k])))\n",
    "#                             candidate_list.append((k, cur_out_of_range))\n",
    "\n",
    "#                     if len(candidate_list) == 0:\n",
    "#                         continue\n",
    "#                     #candidate_list.sort(key=lambda x:x[1])\n",
    "#                     path = path_dict.get((i-1, j), Elem((i-1, j), pre_contours[j], t, pre_out_of_range))\n",
    "#                     temp_path_list = [copy.deepcopy(path) for s in range(len(candidate_list))]\n",
    "#                     put_back_flag = False\n",
    "#                     for s, candidate in enumerate(candidate_list):\n",
    "#                         k = candidate[0]\n",
    "#                         cur_out_of_range = candidate[1]\n",
    "#                         if get_intersection(patient_cut[i], pre_contours[j], cur_contours[k]) < 0.6:\n",
    "#                             continue\n",
    "#                         temp_path_list[s].append((i, k), cur_contours[k], cur_out_of_range)\n",
    "#                         if not (i, k) in path_dict \\\n",
    "#                         or path_dict[(i, k)].out_of_range and not temp_path_list[s].out_of_range \\\n",
    "#                         or len(path_dict[(i, k)]) < len(temp_path_list[s]) \\\n",
    "#                         or path_dict[(i, k)].get_prev_area() < temp_path_list[s].get_prev_area():\n",
    "#                             path_dict[(i, k)] = temp_path_list[s]\n",
    "#                             put_back_flag = True\n",
    "#                     if not put_back_flag:\n",
    "#                         path_dict[(i-1, j)] = path\n",
    "\n",
    "#                 pre_contours = cur_contours\n",
    "#                 pre_circle = cur_circle\n",
    "\n",
    "#             path_list = list(path_dict.values())\n",
    "#             all_path_list += path_list\n",
    "\n",
    "#         print('all_path_list length:', len(all_path_list))\n",
    "#         if len(all_path_list) == 0:\n",
    "#             print(\"Can't find aorta!\")\n",
    "#             continue_flag = True\n",
    "#             break\n",
    "#         all_path_list.sort(key=lambda x: len(x), reverse=True)\n",
    "#         for i in range(min(3, len(all_path_list))):\n",
    "#             print(all_path_list[i].t, ':', len(all_path_list[i]), all_path_list[i].root, '->', all_path_list[i].end, all_path_list[i].out_of_range)\n",
    "#         path = None\n",
    "#         for tp in all_path_list:\n",
    "#             if tp.out_of_range == False:\n",
    "#                 path = tp\n",
    "#                 break\n",
    "#         length_factor = 0.6\n",
    "#         if patient_pixels.shape[0] < 300:\n",
    "#             length_factor = 0.45\n",
    "#         if len(path) < length_factor*patient_pixels.shape[0] \\\n",
    "#         or path.root[0] > crown_start or path.end[0]+1 < aorta_end:#or all_path_list[0].root[0] > 0.2*patient_pixels.shape[0] \\\n",
    "#             delta += 0.05\n",
    "#             all_path_list = []\n",
    "#             if delta > 0.35:\n",
    "#                 print(\"Can't find aorta!\")\n",
    "#                 continue_flag = True\n",
    "#                 break\n",
    "#         else:\n",
    "#             break\n",
    "                \n",
    "#     if continue_flag:\n",
    "#         continue\n",
    "    \n",
    "#     start, end = path.root[0], path.end[0]+1\n",
    "#     path.get_bboxes()\n",
    "    \n",
    "#     branch_start_ori = branch_start\n",
    "#     branch_start += 3\n",
    "#     pre_w = path.bboxes[branch_start-start][2]-path.bboxes[branch_start-start][0]+1\n",
    "#     while branch_start > branch_start_ori-3:\n",
    "#         cur_w = path.bboxes[branch_start-start-1][2]-path.bboxes[branch_start-start-1][0]+1\n",
    "#         if cur_w - pre_w > 0.4 * pre_w:\n",
    "#             break\n",
    "#         branch_start -= 1\n",
    "#         pre_w = cur_w\n",
    "#     if branch_start == branch_start_ori-3:\n",
    "#         branch_start = branch_start_ori\n",
    "    \n",
    "#     get_branch = True\n",
    "#     try:\n",
    "#         assert branch_start > crown_start\n",
    "#         branch_path_list = []\n",
    "#         for tmp_path in all_path_list:\n",
    "#             if tmp_path.t != path.t or tmp_path.root != path.root or len(tmp_path) < branch_start - start + 1:\n",
    "#                 continue\n",
    "#             if tmp_path.keys[branch_start-start-1] == path.keys[branch_start-start-1] and tmp_path.keys[branch_start-start] != path.keys[branch_start-start]:\n",
    "#                 branch_path_list.append(tmp_path)\n",
    "#         print(f'len(branch_path_list):{len(branch_path_list)}')\n",
    "#         branch = branch_path_list[0]\n",
    "#         find_branch_flag = False\n",
    "#         for tb in branch_path_list:\n",
    "#             print(f'len(tb):{tb}')\n",
    "#             tb.get_bboxes()\n",
    "#             tb_start_w = 0\n",
    "#             for tb_i in range(min(5, len(tb)-branch_start+start)):\n",
    "#                 tb_start_w += tb.bboxes[branch_start-start+tb_i][2]-tb.bboxes[branch_start-start+tb_i][0]+1\n",
    "#             tb_start_w /= tb_i+1\n",
    "#             tb_start_x = tb_pre_x = (tb.bboxes[branch_start-start][0]+tb.bboxes[branch_start-start][2])//2\n",
    "#             tb_pre_y = (tb.bboxes[branch_start-start][1]+tb.bboxes[branch_start-start][3])//2\n",
    "#             tb_pre_w = tb.bboxes[branch_start-start][2]-tb.bboxes[branch_start-start][0]+1\n",
    "#             tb_i = branch_start+1\n",
    "#             break_flag = False\n",
    "#             while tb_i < tb.end[0]+1:\n",
    "#                 tb_cur_w = tb.bboxes[tb_i-start][2]-tb.bboxes[tb_i-start][0]+1\n",
    "#                 tb_cur_x = (tb.bboxes[tb_i-start][2]+tb.bboxes[tb_i-start][0])/2\n",
    "#                 tb_cur_y = (tb.bboxes[tb_i-start][3]+tb.bboxes[tb_i-start][1])/2\n",
    "#                 if tb_cur_y > tb_pre_y and tb_cur_x < tb_pre_x and tb_pre_w - tb_cur_w > tb_cur_w * 0.45:\n",
    "#                     break\n",
    "#                 if tb_i-branch_start > 1 and tb_cur_x > tb_start_x and abs(tb_cur_w - tb_start_w) > 0.4 * tb_start_w:\n",
    "#                     break_flag = True\n",
    "#                     break\n",
    "#                 tb_pre_w, tb_pre_x, tb_pre_y = tb_cur_w, tb_cur_x, tb_cur_y\n",
    "#                 tb_i += 1\n",
    "#             if break_flag:\n",
    "#                 branch = tb\n",
    "#                 branch_end = tb_i\n",
    "#                 find_branch_flag = True\n",
    "#                 break\n",
    "        \n",
    "#         if not find_branch_flag:\n",
    "#             branch.get_bboxes()\n",
    "#             branch_end = branch_start+1\n",
    "#             branch_start_w = 0\n",
    "#             for b_i in range(min(5, len(branch)-branch_start+start)):\n",
    "#                 branch_start_w += branch.bboxes[branch_start-start+b_i][2]-branch.bboxes[branch_start-start+b_i][0]+1\n",
    "#             branch_start_w /= b_i+1\n",
    "#             branch_start_x = pre_x = (branch.bboxes[branch_start-start][0]+branch.bboxes[branch_start-start][2])//2\n",
    "#             pre_y = (branch.bboxes[branch_start-start][1]+branch.bboxes[branch_start-start][3])//2\n",
    "#             pre_w = branch.bboxes[branch_start-start][2]-branch.bboxes[branch_start-start][0]+1\n",
    "#             while branch_end < branch.end[0]+1:\n",
    "#                 cur_w = branch.bboxes[branch_end-start][2]-branch.bboxes[branch_end-start][0]+1\n",
    "#                 cur_x = (branch.bboxes[branch_end-start][2]+branch.bboxes[branch_end-start][0])/2\n",
    "#                 cur_y = (branch.bboxes[branch_end-start][3]+branch.bboxes[branch_end-start][1])/2\n",
    "#                 if branch_end-branch_start > 1 and (cur_x > branch_start_x and abs(cur_w - branch_start_w) > 0.4 * branch_start_w \\\n",
    "#                 or cur_y > pre_y and cur_x < pre_x and pre_w - cur_w > cur_w * 0.45):\n",
    "#                     break\n",
    "#                 branch_end += 1\n",
    "#                 pre_w, pre_x, pre_y = cur_w, cur_x, cur_y\n",
    "#         assert branch_end < end\n",
    "#     except:\n",
    "#         traceback.print_exc()\n",
    "#         branch = all_path_list[-1]\n",
    "#         get_branch = False\n",
    "#         branch_start = branch_end = -1\n",
    "#     print(f'crown_start:{crown_start}, aorta_end:{aorta_end}, branch_start:{branch_start}, branch_end:{branch_end}')\n",
    "    \n",
    "#     patient = load_scan(input_folder_list[p])\n",
    "#     draw_bbox(patient, path, start_x, start_y, branch, branch_start, branch_end, crown_start, aorta_end, os.path.join(input_folder_list[p], 'save_raw'))\n",
    "    \n",
    "#     path.blur_bboxes(kind=0)\n",
    "#     if get_branch:\n",
    "#         branch.blur_bboxes(kind=0)\n",
    "# #     patient = load_scan(input_folder_list[p])\n",
    "# #     draw_bbox(patient, path, start_x, start_y, branch, branch_start, branch_end, crown_start, aorta_end, os.path.join(input_folder_list[p], 'save_raw_blur'))\n",
    "    \n",
    "#     path.post_process(patient_pixels, start_x, start_y)\n",
    "#     if get_branch:\n",
    "#         branch.post_process(patient_pixels, start_x, start_y)\n",
    "# #     patient = load_scan(input_folder_list[p])\n",
    "# #     draw_post_bbox(patient, path, branch, branch_start, branch_end, crown_start, aorta_end, os.path.join(input_folder_list[p], 'save_post'))\n",
    "    \n",
    "#     path.blur_bboxes(kind=1)\n",
    "#     if get_branch:\n",
    "#         branch.blur_bboxes(kind=1)\n",
    "#     patient = load_scan(input_folder_list[p])\n",
    "#     draw_post_bbox(patient, path, branch, branch_start, branch_end, crown_start, aorta_end, os.path.join(input_folder_list[p], 'save_processed'))\n",
    "#     patient = load_scan(input_folder_list[p])\n",
    "#     output_image_and_label(patient, path, branch, branch_start, branch_end, crown_start, detection_end, input_folder_list[p])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f3f625c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# def draw_bbox(slices, path, start_x, start_y, branch, branch_start, branch_end, crown_start, aorta_end, save_path):\n",
    "#     sl = save_path.split('/')\n",
    "#     name = sl[-3] if sl[-1] != '' else sl[-4]\n",
    "#     if not os.path.exists(save_path):\n",
    "#         os.mkdir(save_path)\n",
    "#     if not os.path.exists(os.path.join(save_path, 'picture')):\n",
    "#         os.mkdir(os.path.join(save_path, 'picture'))\n",
    "#     start = path.root[0]\n",
    "#     img_height, img_width = slices[0].pixel_array.shape\n",
    "#     for i, s in enumerate(slices):\n",
    "#         img = s.pixel_array\n",
    "#         intercept = slices[i].RescaleIntercept\n",
    "#         slope = slices[i].RescaleSlope\n",
    "#         if crown_start <= i < aorta_end:\n",
    "#             x1, y1 = path.bboxes[i-start][0] + start_x, path.bboxes[i-start][1] + start_y\n",
    "#             x2, y2 = path.bboxes[i-start][2] + start_x, path.bboxes[i-start][3] + start_y\n",
    "#             if x1>=0 and y1>=0 and x2<=img_width and y2<=img_height:\n",
    "#                 picture = img[y1:y2+1, x1:x2+1].astype(np.int16)\n",
    "#                 if slope != 1:\n",
    "#                     picture = (slope * picture.astype(np.float64)).astype(np.int16)\n",
    "#                 picture += np.int16(intercept)\n",
    "#                 picture = np.clip(picture, -100, 500)\n",
    "#                 picture = ((picture+100)/600*255).astype(np.uint8)\n",
    "#                 picture = Image.fromarray(picture)\n",
    "#                 picture.save(os.path.join(save_path, f'picture/1_{name}_{i}.png'))\n",
    "#         if branch_start <= i < branch_end:\n",
    "#             x1, y1 = branch.bboxes[i-start][0] + start_x, branch.bboxes[i-start][1] + start_y\n",
    "#             x2, y2 = branch.bboxes[i-start][2] + start_x, branch.bboxes[i-start][3] + start_y\n",
    "#             if x1>=0 and y1>=0 and x2<=img_width and y2<=img_height:\n",
    "#                 picture = img[y1:y2+1, x1:x2+1].astype(np.int16)\n",
    "#                 if slope != 1:\n",
    "#                     picture = (slope * picture.astype(np.float64)).astype(np.int16)\n",
    "#                 picture += np.int16(intercept)\n",
    "#                 picture = np.clip(picture, -100, 500)\n",
    "#                 picture = ((picture+100)/600*255).astype(np.uint8)\n",
    "#                 picture = Image.fromarray(picture)\n",
    "#                 picture.save(os.path.join(save_path, f'picture/2_{name}_{i}.png'))\n",
    "#         if path.root[0] <= i <= path.end[0]:\n",
    "#             x1, y1 = path.bboxes[i-start][0] + start_x, path.bboxes[i-start][1] + start_y\n",
    "#             x2, y2 = path.bboxes[i-start][2] + start_x, path.bboxes[i-start][3] + start_y\n",
    "#             cv2.rectangle(img, (x1, y1), (x2, y2), 3000, 1)\n",
    "#             if branch_start <= i < branch_end:\n",
    "#                 x1, y1 = branch.bboxes[i-start][0] + start_x, branch.bboxes[i-start][1] + start_y\n",
    "#                 x2, y2 = branch.bboxes[i-start][2] + start_x, branch.bboxes[i-start][3] + start_y\n",
    "#                 cv2.rectangle(img, (x1, y1), (x2, y2), 3000, 1)\n",
    "#         s.PixelData = pydicom.encaps.encapsulate([img.tobytes()])\n",
    "#         s.file_meta.TransferSyntaxUID = '1.2.840.10008.1.2.1'\n",
    "#         s.save_as(os.path.join(save_path, f'{i}.dcm'), write_like_original=False)\n",
    "        \n",
    "# def draw_post_bbox(slices, path, branch, branch_start, branch_end, crown_start, aorta_end, save_path):\n",
    "#     sl = save_path.split('/')\n",
    "#     name = sl[-3] if sl[-1] != '' else sl[-4]\n",
    "#     if not os.path.exists(save_path):\n",
    "#         os.mkdir(save_path)\n",
    "#     if not os.path.exists(os.path.join(save_path, 'picture')):\n",
    "#         os.mkdir(os.path.join(save_path, 'picture'))\n",
    "#     start = path.root[0]\n",
    "#     img_height, img_width = slices[0].pixel_array.shape\n",
    "#     for i, s in enumerate(slices):\n",
    "#         img = s.pixel_array\n",
    "#         intercept = slices[i].RescaleIntercept\n",
    "#         slope = slices[i].RescaleSlope\n",
    "#         if crown_start <= i < aorta_end:\n",
    "#             x1, y1 = path.post_bboxes[i-start][0], path.post_bboxes[i-start][1]\n",
    "#             x2, y2 = path.post_bboxes[i-start][2], path.post_bboxes[i-start][3]\n",
    "#             if x1>=0 and y1>=0 and x2<=img_width and y2<=img_height:\n",
    "#                 picture = img[y1:y2+1, x1:x2+1].astype(np.int16)\n",
    "#                 if slope != 1:\n",
    "#                     picture = (slope * picture.astype(np.float64)).astype(np.int16)\n",
    "#                 picture += np.int16(intercept)\n",
    "#                 picture = np.clip(picture, -100, 500)\n",
    "#                 picture = ((picture+100)/600*255).astype(np.uint8)\n",
    "#                 picture = Image.fromarray(picture)\n",
    "#                 picture.save(os.path.join(save_path, f'picture/1_{name}_{i}.png'))\n",
    "#         if branch_start <= i < branch_end:\n",
    "#             x1, y1 = branch.post_bboxes[i-start][0], branch.post_bboxes[i-start][1]\n",
    "#             x2, y2 = branch.post_bboxes[i-start][2], branch.post_bboxes[i-start][3]\n",
    "#             if x1>=0 and y1>=0 and x2<=img_width and y2<=img_height:\n",
    "#                 picture = img[y1:y2+1, x1:x2+1].astype(np.int16)\n",
    "#                 if slope != 1:\n",
    "#                     picture = (slope * picture.astype(np.float64)).astype(np.int16)\n",
    "#                 picture += np.int16(intercept)\n",
    "#                 picture = np.clip(picture, -100, 500)\n",
    "#                 picture = ((picture+100)/600*255).astype(np.uint8)\n",
    "#                 picture = Image.fromarray(picture)\n",
    "#                 picture.save(os.path.join(save_path, f'picture/2_{name}_{i}.png'))\n",
    "#         if path.root[0] <= i <= path.end[0]:\n",
    "#             x1, y1 = path.post_bboxes[i-start][0], path.post_bboxes[i-start][1]\n",
    "#             x2, y2 = path.post_bboxes[i-start][2], path.post_bboxes[i-start][3]\n",
    "#             cv2.rectangle(img, (x1, y1), (x2, y2), 3000, 1)\n",
    "#             if branch_start <= i < branch_end:\n",
    "#                 x1, y1 = branch.post_bboxes[i-start][0], branch.post_bboxes[i-start][1]\n",
    "#                 x2, y2 = branch.post_bboxes[i-start][2], branch.post_bboxes[i-start][3]\n",
    "#                 cv2.rectangle(img, (x1, y1), (x2, y2), 3000, 1)\n",
    "#         s.PixelData = pydicom.encaps.encapsulate([img.tobytes()])\n",
    "#         s.file_meta.TransferSyntaxUID = '1.2.840.10008.1.2.1'\n",
    "#         s.save_as(os.path.join(save_path, f'{i}.dcm'), write_like_original=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "674ec9b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/chuguangzhou-Im46-111/2/  ******\n",
    "sequence length: 698, height: 512, width: 512\n",
    "find spine area: 798.0\n",
    "times:0.49999999999999994\n",
    "all_path_list length: 8588\n",
    "2 : 223 (475, 0) -> (697, 2)\n",
    "2 : 222 (475, 0) -> (696, 1)\n",
    "2 : 221 (475, 0) -> (695, 1)\n",
    "all_path_list length: 14169\n",
    "2 : 261 (25, 13) -> (285, 18)\n",
    "2 : 260 (25, 13) -> (284, 22)\n",
    "2 : 259 (25, 13) -> (283, 23)\n",
    "all_path_list length: 19922\n",
    "0 : 490 (44, 19) -> (533, 3)\n",
    "0 : 489 (44, 19) -> (532, 4)\n",
    "0 : 488 (44, 19) -> (531, 4)\n",
    "crown_start:94, aorta_end:340, branch_start:96, branch_end:98\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/hujiaguo-Im34-58/2/  ******\n",
    "sequence length: 666, height: 512, width: 512\n",
    "find spine area: 573.5\n",
    "times:0.39999999999999997\n",
    "all_path_list length: 15305\n",
    "2 : 594 (57, 8) -> (650, 11)\n",
    "2 : 593 (57, 8) -> (649, 12)\n",
    "2 : 592 (57, 8) -> (648, 15)\n",
    "crown_start:74, aorta_end:280, branch_start:97, branch_end:183\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/modehe-Im34-89/2/  ******\n",
    "sequence length: 301, height: 512, width: 512\n",
    "find spine area: 2515.5\n",
    "times:0.44999999999999996\n",
    "all_path_list length: 7827\n",
    "2 : 265 (36, 19) -> (300, 14)\n",
    "0 : 264 (37, 18) -> (300, 15)\n",
    "1 : 264 (37, 19) -> (300, 13)\n",
    "crown_start:60, aorta_end:270, branch_start:90, branch_end:91\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/wanglianlian-Im42-139/2/  ******\n",
    "sequence length: 690, height: 512, width: 512\n",
    "find spine area: 3090.5\n",
    "times:0.15000000000000002\n",
    "all_path_list length: 12074\n",
    "0 : 299 (272, 10) -> (570, 5)\n",
    "2 : 299 (273, 4) -> (571, 9)\n",
    "0 : 298 (272, 10) -> (569, 4)\n",
    "all_path_list length: 16301\n",
    "2 : 622 (68, 18) -> (689, 20)\n",
    "2 : 621 (68, 18) -> (688, 15)\n",
    "2 : 620 (68, 18) -> (687, 15)\n",
    "crown_start:123, aorta_end:444, branch_start:135, branch_end:219\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/yuanenchun-Im27-93/2/  ******\n",
    "sequence length: 279, height: 512, width: 512\n",
    "find spine area: 1754.0\n",
    "times:0.2\n",
    "all_path_list length: 7018\n",
    "2 : 189 (26, 12) -> (214, 2)\n",
    "2 : 188 (26, 12) -> (213, 8)\n",
    "2 : 187 (26, 12) -> (212, 6)\n",
    "crown_start:37, aorta_end:180, branch_start:42, branch_end:57\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/yuliyao-Im25-31/2/  ******\n",
    "sequence length: 672, height: 512, width: 512\n",
    "find spine area: 993.0\n",
    "times:0.39999999999999997\n",
    "all_path_list length: 16236\n",
    "2 : 451 (35, 7) -> (485, 10)\n",
    "2 : 450 (35, 7) -> (484, 11)\n",
    "0 : 449 (36, 6) -> (484, 12)\n",
    "crown_start:73, aorta_end:93, branch_start:91, branch_end:119\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/zhangxiyuan-Im9-50/2/  ******\n",
    "sequence length: 221, height: 512, width: 512\n",
    "find spine area: 4452.0\n",
    "times:0.10000000000000002\n",
    "all_path_list length: 3014\n",
    "2 : 155 (10, 6) -> (164, 13)\n",
    "2 : 154 (10, 6) -> (163, 11)\n",
    "2 : 153 (10, 6) -> (162, 12)\n",
    "crown_start:22, aorta_end:100, branch_start:30, branch_end:49\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta_CTA/zhaoqifeng/img/  ******\n",
    "sequence length: 651, height: 512, width: 512\n",
    "find spine area: 600.0\n",
    "times:0.5499999999999999\n",
    "all_path_list length: 12206\n",
    "2 : 583 (68, 10) -> (650, 6)\n",
    "0 : 582 (69, 11) -> (650, 6)\n",
    "2 : 582 (68, 10) -> (649, 8)\n",
    "crown_start:86, aorta_end:502, branch_start:114, branch_end:156\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aortic_dissection/CTA3(+)/+C/  ******\n",
    "sequence length: 707, height: 512, width: 512\n",
    "find spine area: 621.5\n",
    "times:0.5499999999999999\n",
    "all_path_list length: 10680\n",
    "2 : 707 (0, 13) -> (706, 9)\n",
    "2 : 706 (0, 13) -> (705, 6)\n",
    "2 : 705 (0, 13) -> (704, 10)\n",
    "crown_start:31, aorta_end:495, branch_start:49, branch_end:105"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "225de0f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "******Processing: /nfs3-p1/zsxm/dataset/aorta_CTA/chenxiu/img/  ******\n",
    "sequence length: 737, height: 512, width: 512\n",
    "find spine area: 783.5\n",
    "times:0.3\n",
    "all_path_list length: 4414\n",
    "2 : 737 (0, 15) -> (736, 10)\n",
    "0 : 687 (0, 6) -> (686, 5)\n",
    "1 : 687 (0, 3) -> (686, 2)\n",
    "crown_start:53, aorta_end:566, branch_start:82, branch_end:185\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta_CTA/dingxing/img/  ******\n",
    "sequence length: 521, height: 512, width: 512\n",
    "find spine area: 936.0\n",
    "times:0.49999999999999994\n",
    "all_path_list length: 2598\n",
    "0 : 465 (56, 12) -> (520, 15)\n",
    "1 : 438 (83, 6) -> (520, 15)\n",
    "1 : 378 (83, 6) -> (460, 15)\n",
    "crown_start:141, aorta_end:520, branch_start:167, branch_end:269\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta_CTA/huanggaoqiang/img/  ******\n",
    "sequence length: 532, height: 512, width: 512\n",
    "find spine area: 649.5\n",
    "times:0.5499999999999999\n",
    "all_path_list length: 2753\n",
    "2 : 504 (28, 8) -> (531, 7)\n",
    "2 : 504 (28, 8) -> (531, 8)\n",
    "0 : 467 (65, 13) -> (531, 5)\n",
    "crown_start:81, aorta_end:510, branch_start:105, branch_end:108\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta_CTA/louhangqing/img/  ******\n",
    "sequence length: 984, height: 512, width: 512\n",
    "find spine area: 680.5\n",
    "times:0.5499999999999999\n",
    "all_path_list length: 5515\n",
    "2 : 804 (62, 11) -> (865, 20)\n",
    "0 : 802 (58, 5) -> (859, 5)\n",
    "2 : 786 (62, 11) -> (847, 15)\n",
    "crown_start:103, aorta_end:615, branch_start:128, branch_end:148\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta_CTA/wangxiaowei/yes/  ******\n",
    "sequence length: 841, height: 512, width: 512\n",
    "find spine area: 2160.0\n",
    "times:0.49999999999999994\n",
    "all_path_list length: 6698\n",
    "0 : 791 (50, 11) -> (840, 8)\n",
    "1 : 781 (60, 12) -> (840, 5)\n",
    "2 : 774 (67, 21) -> (840, 11)\n",
    "crown_start:110, aorta_end:648, branch_start:136, branch_end:154\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta_CTA/xiangxijiao/img/  ******\n",
    "sequence length: 719, height: 512, width: 512\n",
    "find spine area: 803.0\n",
    "times:0.35\n",
    "all_path_list length: 3769\n",
    "2 : 555 (0, 11) -> (554, 17)\n",
    "2 : 528 (0, 11) -> (527, 9)\n",
    "0 : 521 (0, 13) -> (520, 9)\n",
    "crown_start:52, aorta_end:445, branch_start:66, branch_end:70\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta_CTA/yangxiangqin/yes/  ******\n",
    "sequence length: 808, height: 512, width: 512\n",
    "find spine area: 2117.5\n",
    "times:0.15000000000000002\n",
    "all_path_list length: 4098\n",
    "0 : 679 (0, 3) -> (678, 5)\n",
    "1 : 645 (0, 1) -> (644, 0)\n",
    "0 : 553 (0, 3) -> (552, 7)\n",
    "crown_start:43, aorta_end:483, branch_start:62, branch_end:179\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta_CTA/zhaoqifeng/img/  ******\n",
    "sequence length: 651, height: 512, width: 512\n",
    "find spine area: 600.0\n",
    "times:0.5499999999999999\n",
    "all_path_list length: 3204\n",
    "2 : 583 (68, 10) -> (650, 6)\n",
    "0 : 582 (69, 11) -> (650, 6)\n",
    "0 : 535 (69, 11) -> (603, 11)\n",
    "crown_start:86, aorta_end:502, branch_start:-1, branch_end:-1\n",
    "Traceback (most recent call last):\n",
    "  File \"<ipython-input-14-24d075117625>\", line 163, in <module>\n",
    "    branch = branch_path_list[0]\n",
    "IndexError: list index out of range\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta_CTA/zhujingchun/img/  ******\n",
    "sequence length: 807, height: 512, width: 512\n",
    "find spine area: 676.5\n",
    "times:0.35\n",
    "all_path_list length: 5179\n",
    "0 : 807 (0, 10) -> (806, 11)\n",
    "2 : 807 (0, 15) -> (806, 9)\n",
    "2 : 750 (0, 15) -> (749, 20)\n",
    "crown_start:82, aorta_end:532, branch_start:115, branch_end:140\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta_CTA/zuochun/img/  ******\n",
    "sequence length: 961, height: 512, width: 512\n",
    "find spine area: 1128.5\n",
    "times:0.25\n",
    "all_path_list length: 4740\n",
    "2 : 683 (50, 9) -> (732, 10)\n",
    "2 : 662 (50, 9) -> (711, 13)\n",
    "2 : 642 (50, 9) -> (691, 12)\n",
    "crown_start:99, aorta_end:607, branch_start:115, branch_end:168\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aortic_dissection/CTA1(+)/+C/  ******\n",
    "sequence length: 513, height: 512, width: 512\n",
    "find spine area: 672.0\n",
    "times:0.49999999999999994\n",
    "all_path_list length: 3212\n",
    "0 : 456 (0, 7) -> (455, 5)\n",
    "2 : 456 (0, 14) -> (455, 17)\n",
    "2 : 431 (0, 14) -> (430, 11)\n",
    "crown_start:61, aorta_end:266, branch_start:74, branch_end:140\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aortic_dissection/CTA2(+)/+C/  ******\n",
    "sequence length: 642, height: 512, width: 512\n",
    "find spine area: 795.0\n",
    "times:0.44999999999999996\n",
    "all_path_list length: 4250\n",
    "0 : 597 (45, 11) -> (641, 6)\n",
    "2 : 468 (40, 14) -> (507, 6)\n",
    "0 : 463 (45, 11) -> (507, 9)\n",
    "crown_start:116, aorta_end:467, branch_start:152, branch_end:249\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aortic_dissection/CTA2(-)/+c/  ******\n",
    "sequence length: 807, height: 512, width: 512\n",
    "find spine area: 676.5\n",
    "times:0.35\n",
    "all_path_list length: 5179\n",
    "0 : 807 (0, 10) -> (806, 11)\n",
    "2 : 807 (0, 15) -> (806, 9)\n",
    "2 : 750 (0, 15) -> (749, 20)\n",
    "crown_start:80, aorta_end:573, branch_start:115, branch_end:140\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aortic_dissection/CTA3(+)/+C/  ******\n",
    "sequence length: 707, height: 512, width: 512\n",
    "find spine area: 621.5\n",
    "times:0.5499999999999999\n",
    "all_path_list length: 3551\n",
    "2 : 707 (0, 13) -> (706, 9)\n",
    "2 : 570 (0, 13) -> (569, 12)\n",
    "2 : 284 (0, 13) -> (283, 6)\n",
    "crown_start:31, aorta_end:495, branch_start:-1, branch_end:-1\n",
    "Traceback (most recent call last):\n",
    "  File \"<ipython-input-14-24d075117625>\", line 163, in <module>\n",
    "    branch = branch_path_list[0]\n",
    "IndexError: list index out of range\n",
    "\n",
    "\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/.complete/zhoujun-Im33-128/2/  ******\n",
    "sequence length: 253, height: 512, width: 512\n",
    "find spine area: 560.5\n",
    "times:0.5499999999999999\n",
    "all_path_list length: 2155\n",
    "2 : 216 (28, 11) -> (243, 11)\n",
    "2 : 214 (28, 11) -> (241, 10)\n",
    "0 : 166 (31, 9) -> (196, 7)\n",
    "crown_start:35, aorta_end:133, branch_start:41, branch_end:50\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/.complete/zhouzheyu-Im32-152/2/  ******\n",
    "sequence length: 703, height: 512, width: 512\n",
    "find spine area: 598.0\n",
    "times:0.25\n",
    "all_path_list length: 5384\n",
    "2 : 535 (5, 14) -> (539, 19)\n",
    "2 : 535 (5, 14) -> (539, 17)\n",
    "2 : 531 (5, 14) -> (535, 10)\n",
    "crown_start:88, aorta_end:440, branch_start:112, branch_end:113\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/.complete/zhudehong-Im22-91/2/  ******\n",
    "sequence length: 650, height: 512, width: 512\n",
    "find spine area: 2422.0\n",
    "times:0.44999999999999996\n",
    "all_path_list length: 3076\n",
    "0 : 530 (82, 8) -> (611, 6)\n",
    "2 : 504 (120, 6) -> (623, 6)\n",
    "2 : 498 (120, 6) -> (617, 10)\n",
    "crown_start:98, aorta_end:435, branch_start:108, branch_end:130\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/.complete/zhuyongbu-im23-92/2/  ******\n",
    "sequence length: 308, height: 666, width: 512\n",
    "find spine area: 4623.0\n",
    "times:0.6\n",
    "all_path_list length: 1770\n",
    "2 : 96 (141, 16) -> (236, 18)\n",
    "2 : 96 (212, 2) -> (307, 2)\n",
    "2 : 93 (141, 16) -> (233, 19)\n",
    "all_path_list length: 3062\n",
    "2 : 245 (11, 20) -> (255, 7)\n",
    "2 : 243 (11, 20) -> (253, 14)\n",
    "0 : 230 (24, 26) -> (253, 13)\n",
    "crown_start:37, aorta_end:210, branch_start:51, branch_end:89\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/.complete/chuguangzhou-Im46-111/2/  ******\n",
    "sequence length: 698, height: 512, width: 512\n",
    "find spine area: 798.0\n",
    "times:0.49999999999999994\n",
    "all_path_list length: 1981\n",
    "2 : 223 (475, 0) -> (697, 2)\n",
    "2 : 207 (79, 9) -> (285, 13)\n",
    "2 : 203 (79, 9) -> (281, 17)\n",
    "all_path_list length: 3072\n",
    "2 : 261 (25, 13) -> (285, 18)\n",
    "2 : 257 (25, 13) -> (281, 22)\n",
    "2 : 240 (25, 13) -> (264, 24)\n",
    "all_path_list length: 4496\n",
    "0 : 490 (44, 19) -> (533, 3)\n",
    "0 : 473 (44, 19) -> (516, 7)\n",
    "1 : 444 (79, 3) -> (522, 9)\n",
    "crown_start:94, aorta_end:340, branch_start:-1, branch_end:-1\n",
    "Traceback (most recent call last):\n",
    "  File \"<ipython-input-9-7ed4ab80bf91>\", line 162, in <module>\n",
    "    branch = branch_path_list[0]\n",
    "IndexError: list index out of range\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/.complete/dongxuesheng-Im37-149/2/  ******\n",
    "sequence length: 684, height: 512, width: 512\n",
    "find spine area: 3695.0\n",
    "times:0.15000000000000002\n",
    "all_path_list length: 3343\n",
    "2 : 684 (0, 14) -> (683, 16)\n",
    "2 : 670 (0, 14) -> (669, 7)\n",
    "0 : 613 (44, 9) -> (656, 7)\n",
    "crown_start:80, aorta_end:441, branch_start:96, branch_end:136\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/.complete/durenxi-Im57-172/2/  ******\n",
    "sequence length: 723, height: 512, width: 512\n",
    "find spine area: 5924.5\n",
    "times:0.15000000000000002\n",
    "all_path_list length: 2730\n",
    "0 : 485 (88, 5) -> (572, 2)\n",
    "1 : 484 (88, 4) -> (571, 2)\n",
    "2 : 475 (100, 10) -> (574, 1)\n",
    "crown_start:132, aorta_end:480, branch_start:139, branch_end:167\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/.complete/fangcaiyu-Im21-91/2/  ******\n",
    "sequence length: 267, height: 512, width: 512\n",
    "find spine area: 1856.5\n",
    "times:0.35\n",
    "all_path_list length: 2170\n",
    "2 : 207 (0, 6) -> (206, 10)\n",
    "2 : 193 (0, 6) -> (192, 14)\n",
    "0 : 187 (7, 14) -> (193, 8)\n",
    "crown_start:38, aorta_end:170, branch_start:43, branch_end:64\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/.complete/fengyonghua-Im21-89/2/  ******\n",
    "sequence length: 255, height: 512, width: 512\n",
    "find spine area: 843.5\n",
    "times:0.5499999999999999\n",
    "all_path_list length: 2464\n",
    "0 : 192 (0, 6) -> (191, 12)\n",
    "1 : 192 (0, 7) -> (191, 6)\n",
    "0 : 159 (0, 6) -> (158, 12)\n",
    "crown_start:32, aorta_end:174, branch_start:38, branch_end:72\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/.complete/fuhuajin-Im29-126/2/  ******\n",
    "sequence length: 666, height: 512, width: 512\n",
    "find spine area: 2127.0\n",
    "times:0.44999999999999996\n",
    "all_path_list length: 4420\n",
    "0 : 472 (194, 7) -> (665, 2)\n",
    "0 : 372 (194, 7) -> (565, 8)\n",
    "2 : 344 (193, 9) -> (536, 7)\n",
    "all_path_list length: 6210\n",
    "0 : 486 (180, 13) -> (665, 4)\n",
    "0 : 463 (180, 13) -> (642, 4)\n",
    "0 : 386 (180, 13) -> (565, 10)\n",
    "all_path_list length: 7908\n",
    "0 : 509 (157, 11) -> (665, 6)\n",
    "0 : 486 (157, 11) -> (642, 7)\n",
    "0 : 409 (157, 11) -> (565, 10)\n",
    "all_path_list length: 9451\n",
    "0 : 643 (23, 13) -> (665, 9)\n",
    "0 : 620 (23, 13) -> (642, 9)\n",
    "0 : 543 (23, 13) -> (565, 10)\n",
    "crown_start:53, aorta_end:372, branch_start:73, branch_end:162\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/.complete/gaomingsheng-Im34-151/2/  ******\n",
    "sequence length: 720, height: 512, width: 512\n",
    "find spine area: 1020.0\n",
    "times:0.35\n",
    "all_path_list length: 4011\n",
    "2 : 600 (78, 16) -> (677, 7)\n",
    "0 : 479 (67, 10) -> (545, 16)\n",
    "0 : 443 (67, 10) -> (509, 11)\n",
    "crown_start:102, aorta_end:435, branch_start:122, branch_end:1882.0\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/gengyongchao-Im21-83/2/  ******\n",
    "sequence length: 122, height: 512, width: 512\n",
    "find spine area: 1550.0\n",
    "times:0.6\n",
    "all_path_list length: 1272\n",
    "1 : 91 (3, 11) -> (93, 12)\n",
    "1 : 90 (3, 11) -> (92, 11)\n",
    "0 : 86 (10, 12) -> (95, 11)\n",
    "crown_start:9, aorta_end:81, branch_start:16, branch_end:22\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/hebin-Im41-91/2/  ******\n",
    "sequence length: 270, height: 512, width: 512\n",
    "find spine area: 1415.0\n",
    "times:0.3\n",
    "all_path_list length: 2178\n",
    "0 : 190 (33, 14) -> (222, 8)\n",
    "2 : 150 (44, 10) -> (193, 11)\n",
    "1 : 144 (45, 12) -> (188, 6)\n",
    "crown_start:50, aorta_end:172, branch_start:56, branch_end:66\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/hejianjun-Im31-99/2/  ******\n",
    "sequence length: 639, height: 512, width: 512\n",
    "find spine area: 1908.0\n",
    "times:0.6\n",
    "all_path_list length: 4626\n",
    "2 : 578 (0, 12) -> (577, 9)\n",
    "0 : 577 (0, 9) -> (576, 7)\n",
    "2 : 532 (0, 12) -> (531, 10)\n",
    "crown_start:83, aorta_end:319, branch_start:100, branch_end:137\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/heshengnan-Im16-90/2/  ******\n",
    "sequence length: 253, height: 512, width: 512\n",
    "find spine area: 549.0\n",
    "times:0.49999999999999994\n",
    "all_path_list length: 2729\n",
    "2 : 200 (0, 14) -> (199, 8)\n",
    "2 : 190 (0, 14) -> (189, 15)\n",
    "1 : 187 (3, 7) -> (189, 9)\n",
    "crown_start:33, aorta_end:160, branch_start:38, branch_end:69\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/huangjianhua-Im38-144/2/  ******\n",
    "sequence length: 648, height: 512, width: 512\n",
    "find spine area: 2561.0\n",
    "times:0.25\n",
    "all_path_list length: 3178\n",
    "2 : 406 (242, 2) -> (647, 9)\n",
    "2 : 388 (242, 2) -> (629, 6)\n",
    "2 : 373 (197, 5) -> (569, 21)\n",
    "all_path_list length: 4892\n",
    "0 : 596 (4, 15) -> (599, 9)\n",
    "0 : 586 (4, 15) -> (589, 19)\n",
    "0 : 473 (4, 15) -> (476, 9)\n",
    "crown_start:96, aorta_end:432, branch_start:109, branch_end:189\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/hujiaguo-Im34-58/2/  ******\n",
    "sequence length: 666, height: 512, width: 512\n",
    "find spine area: 573.5\n",
    "times:0.39999999999999997\n",
    "all_path_list length: 3415\n",
    "2 : 594 (57, 8) -> (650, 11)\n",
    "2 : 585 (57, 8) -> (641, 9)\n",
    "0 : 508 (57, 4) -> (564, 8)\n",
    "crown_start:74, aorta_end:280, branch_start:-1, branch_end:-1\n",
    "Traceback (most recent call last):\n",
    "  File \"<ipython-input-6-7ed4ab80bf91>\", line 162, in <module>\n",
    "    branch = branch_path_list[0]\n",
    "IndexError: list index out of range\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/jiangquanfeng-Im 50-107/2/  ******\n",
    "sequence length: 537, height: 512, width: 512\n",
    "find spine area: 1497.0\n",
    "times:0.6\n",
    "all_path_list length: 2321\n",
    "0 : 383 (0, 8) -> (382, 8)\n",
    "1 : 383 (0, 4) -> (382, 9)\n",
    "2 : 383 (0, 8) -> (382, 11)\n",
    "crown_start:64, aorta_end:293, branch_start:82, branch_end:87\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/litingxian-Im36-141/2/  ******\n",
    "sequence length: 618, height: 512, width: 512\n",
    "find spine area: 804.5\n",
    "times:0.6\n",
    "all_path_list length: 2862\n",
    "2 : 271 (347, 8) -> (617, 11)\n",
    "2 : 264 (347, 8) -> (610, 5)\n",
    "2 : 208 (353, 11) -> (560, 12)\n",
    "all_path_list length: 4232\n",
    "2 : 359 (259, 11) -> (617, 11)\n",
    "2 : 359 (259, 11) -> (617, 14)\n",
    "0 : 354 (129, 2) -> (482, 5)\n",
    "all_path_list length: 6245\n",
    "2 : 571 (47, 24) -> (617, 21)\n",
    "2 : 571 (47, 24) -> (617, 26)\n",
    "0 : 498 (29, 13) -> (526, 2)\n",
    "crown_start:75, aorta_end:420, branch_start:92, branch_end:115\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/luohuafa-im29-96/2/  ******\n",
    "sequence length: 323, height: 666, width: 512\n",
    "find spine area: 1336.5\n",
    "times:0.25\n",
    "all_path_list length: 2004\n",
    "2 : 275 (0, 16) -> (274, 11)\n",
    "0 : 236 (4, 10) -> (239, 6)\n",
    "0 : 233 (4, 10) -> (236, 8)\n",
    "crown_start:38, aorta_end:230, branch_start:47, branch_end:80\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/lushida-Im33-159/2/  ******\n",
    "sequence length: 705, height: 512, width: 512\n",
    "find spine area: 712.5\n",
    "times:0.39999999999999997\n",
    "all_path_list length: 3943\n",
    "2 : 383 (105, 2) -> (487, 14)\n",
    "0 : 369 (119, 4) -> (487, 6)\n",
    "1 : 365 (123, 1) -> (487, 5)\n",
    "all_path_list length: 5321\n",
    "2 : 600 (105, 3) -> (704, 22)\n",
    "2 : 592 (105, 3) -> (696, 16)\n",
    "2 : 570 (105, 3) -> (674, 12)\n",
    "all_path_list length: 6306\n",
    "2 : 600 (105, 4) -> (704, 24)\n",
    "2 : 592 (105, 4) -> (696, 18)\n",
    "2 : 570 (105, 4) -> (674, 13)\n",
    "all_path_list length: 7216\n",
    "2 : 600 (105, 4) -> (704, 23)\n",
    "2 : 592 (105, 4) -> (696, 18)\n",
    "2 : 587 (105, 4) -> (691, 14)\n",
    "all_path_list length: 8393\n",
    "2 : 600 (105, 4) -> (704, 24)\n",
    "2 : 592 (105, 4) -> (696, 20)\n",
    "2 : 587 (105, 4) -> (691, 17)\n",
    "all_path_list length: 8491\n",
    "2 : 600 (105, 4) -> (704, 24)\n",
    "2 : 592 (105, 4) -> (696, 20)\n",
    "2 : 587 (105, 4) -> (691, 16)\n",
    "all_path_list length: 8496\n",
    "2 : 600 (105, 4) -> (704, 24)\n",
    "2 : 592 (105, 4) -> (696, 20)\n",
    "2 : 587 (105, 4) -> (691, 16)\n",
    "all_path_list length: 8496\n",
    "2 : 600 (105, 4) -> (704, 24)\n",
    "2 : 592 (105, 4) -> (696, 20)\n",
    "2 : 587 (105, 4) -> (691, 16)\n",
    "Can't find aorta!\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/lvpeiji-Im32-136/2/  ******\n",
    "sequence length: 636, height: 512, width: 512\n",
    "find spine area: 786.0\n",
    "times:0.44999999999999996\n",
    "all_path_list length: 2866\n",
    "2 : 431 (0, 11) -> (430, 11)\n",
    "0 : 430 (0, 8) -> (429, 7)\n",
    "1 : 399 (30, 6) -> (428, 4)\n",
    "crown_start:86, aorta_end:425, branch_start:108, branch_end:123\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/manyanfeng-Im32-153/2/  ******\n",
    "sequence length: 660, height: 512, width: 512\n",
    "find spine area: 620.0\n",
    "times:0.25\n",
    "all_path_list length: 4382\n",
    "2 : 660 (0, 10) -> (659, 20)\n",
    "2 : 633 (0, 10) -> (632, 9)\n",
    "2 : 528 (0, 10) -> (527, 8)\n",
    "crown_start:91, aorta_end:355, branch_start:111, branch_end:117\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/maoliang-Im12-90/2/  ******\n",
    "sequence length: 253, height: 512, width: 512\n",
    "find spine area: 822.5\n",
    "times:0.2\n",
    "all_path_list length: 2140\n",
    "2 : 196 (5, 13) -> (200, 8)\n",
    "0 : 184 (17, 6) -> (200, 6)\n",
    "2 : 173 (5, 13) -> (177, 11)\n",
    "crown_start:18, aorta_end:163, branch_start:33, branch_end:44\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/modehe-Im34-89/2/  ******\n",
    "sequence length: 301, height: 512, width: 512\n",
    "find spine area: 2515.5\n",
    "times:0.44999999999999996\n",
    "all_path_list length: 2093\n",
    "2 : 265 (36, 19) -> (300, 14)\n",
    "0 : 264 (37, 18) -> (300, 15)\n",
    "1 : 264 (37, 19) -> (300, 13)\n",
    "crown_start:60, aorta_end:270, branch_start:-1, branch_end:-1\n",
    "Traceback (most recent call last):\n",
    "  File \"<ipython-input-6-7ed4ab80bf91>\", line 162, in <module>\n",
    "    branch = branch_path_list[0]\n",
    "IndexError: list index out of range\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/nibingshan-Im33-42/2/  ******\n",
    "sequence length: 696, height: 512, width: 512\n",
    "find spine area: 584.5\n",
    "times:0.5499999999999999\n",
    "all_path_list length: 4089\n",
    "2 : 696 (0, 13) -> (695, 13)\n",
    "0 : 684 (0, 9) -> (683, 2)\n",
    "2 : 662 (0, 13) -> (661, 5)\n",
    "crown_start:90, aorta_end:345, branch_start:98, branch_end:99\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/qiquanping-Im25-54/2/  ******\n",
    "sequence length: 1073, height: 512, width: 512\n",
    "find spine area: 679.5\n",
    "times:0.5499999999999999\n",
    "all_path_list length: 6427\n",
    "0 : 734 (0, 16) -> (733, 13)\n",
    "2 : 700 (36, 14) -> (735, 7)\n",
    "0 : 675 (0, 16) -> (674, 12)\n",
    "crown_start:131, aorta_end:655, branch_start:164, branch_end:180\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/shengfumei-Im31-132/2/  ******\n",
    "sequence length: 612, height: 512, width: 512\n",
    "find spine area: 740.5\n",
    "times:0.3\n",
    "all_path_list length: 3518\n",
    "0 : 595 (12, 10) -> (606, 8)\n",
    "2 : 592 (20, 10) -> (611, 17)\n",
    "2 : 571 (20, 10) -> (590, 12)\n",
    "crown_start:86, aorta_end:390, branch_start:94, branch_end:112\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/shenhuagou-Im33-95/2/  ******\n",
    "sequence length: 633, height: 512, width: 512\n",
    "find spine area: 533.5\n",
    "times:0.44999999999999996\n",
    "all_path_list length: 3511\n",
    "2 : 486 (30, 16) -> (515, 8)\n",
    "0 : 480 (32, 11) -> (511, 5)\n",
    "2 : 466 (30, 16) -> (495, 12)\n",
    "crown_start:85, aorta_end:300, branch_start:98, branch_end:165\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/shenjiaren-Im35-136/2/  ******\n",
    "sequence length: 255, height: 512, width: 512\n",
    "find spine area: 1455.0\n",
    "times:0.49999999999999994\n",
    "all_path_list length: 2639\n",
    "2 : 188 (0, 17) -> (187, 13)\n",
    "2 : 184 (0, 17) -> (183, 13)\n",
    "0 : 171 (13, 11) -> (183, 12)\n",
    "crown_start:29, aorta_end:142, branch_start:35, branch_end:63\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/shenjie-Im36-100/2/  ******\n",
    "sequence length: 715, height: 512, width: 512\n",
    "find spine area: 4348.5\n",
    "times:0.35\n",
    "all_path_list length: 2646\n",
    "2 : 368 (298, 7) -> (665, 4)\n",
    "2 : 367 (298, 7) -> (664, 4)\n",
    "2 : 363 (298, 7) -> (660, 6)\n",
    "all_path_list length: 4251\n",
    "2 : 632 (83, 9) -> (714, 14)\n",
    "2 : 596 (83, 9) -> (678, 7)\n",
    "2 : 583 (83, 9) -> (665, 8)\n",
    "crown_start:146, aorta_end:480, branch_start:165, branch_end:185\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/shiaihua-Im28-108/2/  ******\n",
    "sequence length: 643, height: 512, width: 512\n",
    "find spine area: 733.0\n",
    "times:0.35\n",
    "all_path_list length: 3728\n",
    "2 : 461 (12, 13) -> (472, 8)\n",
    "2 : 457 (12, 13) -> (468, 13)\n",
    "2 : 452 (12, 13) -> (463, 12)\n",
    "crown_start:72, aorta_end:314, branch_start:83, branch_end:95\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/shilei-Im21-92/2/  ******\n",
    "sequence length: 1129, height: 512, width: 512\n",
    "find spine area: 667.0\n",
    "times:0.6\n",
    "all_path_list length: 5165\n",
    "0 : 1065 (64, 7) -> (1128, 0)\n",
    "2 : 1065 (64, 18) -> (1128, 13)\n",
    "2 : 1025 (64, 18) -> (1088, 7)\n",
    "crown_start:138, aorta_end:721, branch_start:71, branch_end:75\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/shuzhiming-Im28-80/2/  ******\n",
    "sequence length: 129, height: 512, width: 512\n",
    "find spine area: 692.5\n",
    "find spine area: 2.0\n",
    "times:0.3\n",
    "all_path_list length: 1197\n",
    "0 : 92 (10, 13) -> (101, 12)\n",
    "0 : 88 (10, 13) -> (97, 10)\n",
    "1 : 64 (14, 9) -> (77, 11)\n",
    "crown_start:12, aorta_end:78, branch_start:17, branch_end:18\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/taofangguo-Im30-142/2/  ******\n",
    "sequence length: 639, height: 512, width: 512\n",
    "find spine area: 639.0\n",
    "times:0.3\n",
    "all_path_list length: 4077\n",
    "0 : 484 (47, 16) -> (530, 1)\n",
    "2 : 481 (47, 20) -> (527, 8)\n",
    "2 : 463 (48, 26) -> (510, 14)\n",
    "crown_start:68, aorta_end:412, branch_start:91, branch_end:167\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/wangaishui-Im75-128/2/  ******\n",
    "sequence length: 711, height: 512, width: 512\n",
    "find spine area: 903.0\n",
    "times:0.3\n",
    "all_path_list length: 3038\n",
    "2 : 680 (20, 10) -> (699, 12)\n",
    "2 : 539 (20, 10) -> (558, 7)\n",
    "2 : 497 (20, 10) -> (516, 10)\n",
    "crown_start:140, aorta_end:410, branch_start:149, branch_end:188\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/wanggangxiang-Im16-90/2/  ******\n",
    "sequence length: 745, height: 512, width: 512\n",
    "find spine area: 559.0\n",
    "times:0.5499999999999999\n",
    "all_path_list length: 3964\n",
    "2 : 474 (131, 10) -> (604, 9)\n",
    "2 : 454 (131, 10) -> (584, 11)\n",
    "2 : 448 (131, 10) -> (578, 10)\n",
    "crown_start:168, aorta_end:540, branch_start:184, branch_end:214\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/wanggaosheng-Im24-57/2/  ******\n",
    "sequence length: 135, height: 512, width: 512\n",
    "find spine area: 906.5\n",
    "times:0.15000000000000002\n",
    "all_path_list length: 1110\n",
    "2 : 97 (11, 14) -> (107, 11)\n",
    "0 : 94 (14, 11) -> (107, 7)\n",
    "1 : 92 (14, 15) -> (105, 9)\n",
    "crown_start:16, aorta_end:69, branch_start:25, branch_end:39\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/wangjianjun-Im30-110/2/  ******\n",
    "sequence length: 630, height: 512, width: 512\n",
    "find spine area: 3041.5\n",
    "times:0.3\n",
    "all_path_list length: 2395\n",
    "2 : 598 (0, 13) -> (597, 7)\n",
    "2 : 502 (0, 13) -> (501, 5)\n",
    "2 : 490 (0, 13) -> (489, 5)\n",
    "crown_start:81, aorta_end:331, branch_start:98, branch_end:147\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/wanglianlian-Im42-139/2/  ******\n",
    "sequence length: 690, height: 512, width: 512\n",
    "find spine area: 3090.5\n",
    "times:0.15000000000000002\n",
    "all_path_list length: 2516\n",
    "0 : 299 (272, 10) -> (570, 5)\n",
    "2 : 299 (273, 4) -> (571, 9)\n",
    "2 : 257 (273, 4) -> (529, 3)\n",
    "all_path_list length: 3766\n",
    "2 : 622 (68, 18) -> (689, 20)\n",
    "2 : 559 (68, 18) -> (626, 9)\n",
    "0 : 542 (69, 6) -> (610, 7)\n",
    "crown_start:123, aorta_end:444, branch_start:-1, branch_end:-1\n",
    "Traceback (most recent call last):\n",
    "  File \"<ipython-input-6-7ed4ab80bf91>\", line 162, in <module>\n",
    "    branch = branch_path_list[0]\n",
    "IndexError: list index out of range\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/wangmanying-Im23-134/2/  ******\n",
    "sequence length: 588, height: 512, width: 512\n",
    "find spine area: 2113.5\n",
    "find spine area: 590.0\n",
    "times:0.35\n",
    "all_path_list length: 3249\n",
    "2 : 428 (0, 7) -> (427, 8)\n",
    "2 : 412 (0, 7) -> (411, 13)\n",
    "2 : 409 (0, 7) -> (408, 11)\n",
    "crown_start:53, aorta_end:383, branch_start:64, branch_end:167\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/wangzhongwen-Im36-80/2/  ******\n",
    "sequence length: 133, height: 512, width: 512\n",
    "find spine area: 4727.0\n",
    "times:0.35\n",
    "all_path_list length: 1100\n",
    "2 : 102 (0, 7) -> (101, 11)\n",
    "1 : 91 (15, 9) -> (105, 5)\n",
    "0 : 90 (15, 8) -> (104, 8)\n",
    "crown_start:21, aorta_end:82, branch_start:25, branch_end:27\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/wuguangde-im55-205/2/  ******\n",
    "sequence length: 299, height: 512, width: 512\n",
    "find spine area: 2193.5\n",
    "find spine area: 251.5\n",
    "find spine area: 156.5\n",
    "times:0.35\n",
    "all_path_list length: 2164\n",
    "0 : 202 (59, 14) -> (260, 11)\n",
    "2 : 201 (61, 16) -> (261, 10)\n",
    "2 : 176 (61, 16) -> (236, 14)\n",
    "crown_start:73, aorta_end:200, branch_start:79, branch_end:111\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/xiewenbo-Im25-40/2/  ******\n",
    "sequence length: 1033, height: 512, width: 512\n",
    "find spine area: 532.5\n",
    "times:0.6\n",
    "all_path_list length: 5008\n",
    "2 : 958 (0, 11) -> (957, 7)\n",
    "0 : 921 (14, 7) -> (934, 6)\n",
    "1 : 862 (26, 4) -> (887, 1)\n",
    "crown_start:134, aorta_end:428, branch_start:165, branch_end:245\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/xujinbiao-Im37-146/2/  ******\n",
    "sequence length: 651, height: 512, width: 512\n",
    "find spine area: 804.5\n",
    "times:0.35\n",
    "all_path_list length: 4019\n",
    "2 : 566 (75, 12) -> (640, 8)\n",
    "2 : 438 (75, 12) -> (512, 12)\n",
    "0 : 410 (63, 12) -> (472, 10)\n",
    "crown_start:87, aorta_end:418, branch_start:110, branch_end:120\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/xujinyang-Im15-30/2/  ******\n",
    "sequence length: 139, height: 512, width: 512\n",
    "find spine area: 1226.5\n",
    "times:0.35\n",
    "all_path_list length: 1297\n",
    "0 : 94 (9, 10) -> (102, 8)\n",
    "2 : 93 (9, 10) -> (101, 10)\n",
    "0 : 92 (9, 10) -> (100, 11)\n",
    "crown_start:14, aorta_end:18, branch_start:18, branch_end:37\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/yangwenbin-Im44-125/2/  ******\n",
    "sequence length: 699, height: 512, width: 512\n",
    "find spine area: 537.0\n",
    "times:0.3\n",
    "all_path_list length: 2818\n",
    "2 : 339 (92, 18) -> (430, 13)\n",
    "2 : 316 (92, 18) -> (407, 14)\n",
    "2 : 312 (92, 18) -> (403, 20)\n",
    "all_path_list length: 4112\n",
    "2 : 339 (92, 24) -> (430, 18)\n",
    "2 : 316 (92, 24) -> (407, 18)\n",
    "2 : 309 (92, 24) -> (400, 27)\n",
    "all_path_list length: 5488\n",
    "2 : 517 (0, 12) -> (516, 17)\n",
    "2 : 485 (0, 12) -> (484, 22)\n",
    "2 : 473 (0, 12) -> (472, 16)\n",
    "crown_start:125, aorta_end:388, branch_start:141, branch_end:206\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/yangyoufu-Im16-25/2/  ******\n",
    "sequence length: 122, height: 512, width: 512\n",
    "find spine area: 1817.0\n",
    "times:0.3\n",
    "all_path_list length: 1498\n",
    "0 : 98 (0, 12) -> (97, 3)\n",
    "2 : 92 (7, 17) -> (98, 11)\n",
    "2 : 89 (7, 17) -> (95, 14)\n",
    "crown_start:15, aorta_end:20, branch_start:19, branch_end:37\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/yaoliang-Im30-83/2/  ******\n",
    "sequence length: 289, height: 512, width: 512\n",
    "find spine area: 3250.0\n",
    "times:0.05000000000000002\n",
    "all_path_list length: 2161\n",
    "2 : 79 (101, 5) -> (179, 10)\n",
    "2 : 73 (140, 20) -> (212, 17)\n",
    "0 : 69 (141, 23) -> (209, 13)\n",
    "all_path_list length: 2847\n",
    "2 : 120 (60, 21) -> (179, 10)\n",
    "2 : 102 (60, 21) -> (161, 8)\n",
    "2 : 99 (190, 3) -> (288, 8)\n",
    "all_path_list length: 3567\n",
    "0 : 183 (25, 19) -> (207, 7)\n",
    "2 : 177 (31, 12) -> (207, 6)\n",
    "1 : 159 (31, 10) -> (189, 8)\n",
    "crown_start:35, aorta_end:176, branch_start:43, branch_end:53\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/yinghao-Im18-86/2/  ******\n",
    "sequence length: 215, height: 666, width: 512\n",
    "find spine area: 1762.5\n",
    "times:0.3\n",
    "all_path_list length: 2320\n",
    "2 : 171 (0, 11) -> (170, 15)\n",
    "2 : 158 (0, 11) -> (157, 13)\n",
    "2 : 119 (0, 11) -> (118, 13)\n",
    "crown_start:28, aorta_end:145, branch_start:36, branch_end:58\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/yingzhemin-Im58-108/2/  ******\n",
    "sequence length: 277, height: 512, width: 512\n",
    "find spine area: 1606.0\n",
    "times:0.2\n",
    "all_path_list length: 2214\n",
    "0 : 196 (11, 12) -> (206, 11)\n",
    "0 : 196 (11, 12) -> (206, 8)\n",
    "1 : 195 (11, 12) -> (205, 5)\n",
    "crown_start:23, aorta_end:177, branch_start:32, branch_end:43\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/yuanenchun-Im27-93/2/  ******\n",
    "sequence length: 279, height: 512, width: 512\n",
    "find spine area: 1754.0\n",
    "times:0.2\n",
    "all_path_list length: 2688\n",
    "2 : 189 (26, 12) -> (214, 2)\n",
    "2 : 175 (26, 12) -> (200, 13)\n",
    "1 : 173 (29, 9) -> (201, 18)\n",
    "crown_start:37, aorta_end:180, branch_start:-1, branch_end:-1\n",
    "Traceback (most recent call last):\n",
    "  File \"<ipython-input-6-7ed4ab80bf91>\", line 162, in <module>\n",
    "    branch = branch_path_list[0]\n",
    "IndexError: list index out of range\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/yujinmin-Im25-78/2/  ******\n",
    "sequence length: 977, height: 512, width: 512\n",
    "find spine area: 1882.0\n",
    "times:0.3\n",
    "all_path_list length: 4098\n",
    "0 : 783 (15, 5) -> (797, 2)\n",
    "2 : 753 (47, 16) -> (799, 8)\n",
    "0 : 738 (15, 5) -> (752, 6)\n",
    "crown_start:81, aorta_end:588, branch_start:104, branch_end:234\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/yukongban-Im30-139/2/  ******\n",
    "sequence length: 636, height: 512, width: 512\n",
    "find spine area: 782.0\n",
    "times:0.49999999999999994\n",
    "all_path_list length: 4520\n",
    "2 : 530 (20, 14) -> (549, 15)\n",
    "0 : 527 (22, 13) -> (548, 13)\n",
    "1 : 517 (31, 6) -> (547, 7)\n",
    "crown_start:84, aorta_end:406, branch_start:100, branch_end:159\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/yuliyao-Im25-31/2/  ******\n",
    "sequence length: 672, height: 512, width: 512\n",
    "find spine area: 993.0\n",
    "times:0.39999999999999997\n",
    "all_path_list length: 4150\n",
    "2 : 451 (35, 7) -> (485, 10)\n",
    "0 : 449 (36, 6) -> (484, 12)\n",
    "1 : 448 (36, 7) -> (483, 7)\n",
    "crown_start:73, aorta_end:93, branch_start:-1, branch_end:-1\n",
    "Traceback (most recent call last):\n",
    "  File \"<ipython-input-6-7ed4ab80bf91>\", line 162, in <module>\n",
    "    branch = branch_path_list[0]\n",
    "IndexError: list index out of range\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/zengzhengyou-Im54-145/2/  ******\n",
    "sequence length: 663, height: 512, width: 512\n",
    "find spine area: 2725.5\n",
    "times:0.6\n",
    "all_path_list length: 3082\n",
    "0 : 253 (0, 5) -> (252, 7)\n",
    "0 : 252 (0, 5) -> (251, 11)\n",
    "0 : 250 (0, 5) -> (249, 8)\n",
    "all_path_list length: 4579\n",
    "0 : 494 (0, 11) -> (493, 13)\n",
    "0 : 490 (0, 11) -> (489, 19)\n",
    "0 : 486 (0, 11) -> (485, 7)\n",
    "crown_start:89, aorta_end:428, branch_start:103, branch_end:186\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/zhangxinliang-Im16-24/2/  ******\n",
    "sequence length: 58, height: 512, width: 512\n",
    "find spine area: 1193.0\n",
    "find spine area: 388.0\n",
    "times:0.49999999999999994\n",
    "all_path_list length: 610\n",
    "0 : 51 (7, 15) -> (57, 10)\n",
    "1 : 51 (7, 13) -> (57, 12)\n",
    "2 : 51 (7, 16) -> (57, 12)\n",
    "crown_start:13, aorta_end:18, branch_start:17, branch_end:34\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/zhangxiyuan-Im9-50/2/  ******\n",
    "sequence length: 221, height: 512, width: 512\n",
    "find spine area: 4452.0\n",
    "times:0.10000000000000002\n",
    "all_path_list length: 1437\n",
    "2 : 155 (10, 6) -> (164, 13)\n",
    "2 : 152 (10, 6) -> (161, 9)\n",
    "2 : 146 (10, 6) -> (155, 12)\n",
    "crown_start:22, aorta_end:100, branch_start:-1, branch_end:-1\n",
    "Traceback (most recent call last):\n",
    "  File \"<ipython-input-6-7ed4ab80bf91>\", line 162, in <module>\n",
    "    branch = branch_path_list[0]\n",
    "IndexError: list index out of range\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/zhaojianbin-Im31-158/2/  ******\n",
    "sequence length: 669, height: 512, width: 512\n",
    "find spine area: 2084.5\n",
    "times:0.35\n",
    "all_path_list length: 4421\n",
    "1 : 265 (266, 3) -> (530, 7)\n",
    "2 : 250 (0, 12) -> (249, 10)\n",
    "2 : 243 (0, 12) -> (242, 16)\n",
    "all_path_list length: 5833\n",
    "2 : 489 (0, 14) -> (488, 12)\n",
    "2 : 485 (0, 14) -> (484, 17)\n",
    "2 : 481 (0, 14) -> (480, 15)\n",
    "crown_start:74, aorta_end:435, branch_start:93, branch_end:232\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/zhaojun-Im45-103/2/  ******\n",
    "sequence length: 632, height: 512, width: 512\n",
    "find spine area: 17894.5\n",
    "find spine area: 808.0\n",
    "find spine area: 17.5\n",
    "find spine area: 5.5\n",
    "find spine area: 1.0\n",
    "find spine area: 25370.0\n",
    "times:0.10000000000000002\n",
    "all_path_list length: 3112\n",
    "2 : 227 (0, 11) -> (226, 3)\n",
    "2 : 220 (326, 10) -> (545, 14)\n",
    "2 : 219 (326, 10) -> (544, 15)\n",
    "all_path_list length: 4719\n",
    "2 : 251 (0, 16) -> (250, 11)\n",
    "2 : 251 (0, 16) -> (250, 13)\n",
    "0 : 239 (254, 12) -> (492, 8)\n",
    "all_path_list length: 6125\n",
    "2 : 632 (0, 23) -> (631, 30)\n",
    "2 : 610 (0, 23) -> (609, 22)\n",
    "0 : 569 (38, 18) -> (606, 16)\n",
    "crown_start:54, aorta_end:416, branch_start:121, branch_end:196\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/zhengsiqing-Im35-106/2/  ******\n",
    "sequence length: 666, height: 512, width: 512\n",
    "find spine area: 1991.0\n",
    "times:0.3\n",
    "all_path_list length: 4321\n",
    "2 : 371 (31, 14) -> (401, 18)\n",
    "2 : 355 (31, 14) -> (385, 11)\n",
    "0 : 341 (45, 17) -> (385, 16)\n",
    "all_path_list length: 5992\n",
    "2 : 635 (31, 14) -> (665, 21)\n",
    "2 : 630 (31, 14) -> (660, 18)\n",
    "0 : 596 (45, 22) -> (640, 14)\n",
    "crown_start:99, aorta_end:331, branch_start:114, branch_end:162\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/zhoufei-Im16-97/2/  ******\n",
    "sequence length: 581, height: 512, width: 512\n",
    "find spine area: 1656.0\n",
    "times:0.49999999999999994\n",
    "all_path_list length: 5022\n",
    "0 : 486 (0, 6) -> (485, 8)\n",
    "0 : 430 (0, 6) -> (429, 12)\n",
    "0 : 412 (0, 6) -> (411, 10)\n",
    "crown_start:68, aorta_end:353, branch_start:76, branch_end:133\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/zhoujinhai-Im14-94/2/  ******\n",
    "sequence length: 945, height: 512, width: 512\n",
    "find spine area: 2807.5\n",
    "times:0.44999999999999996\n",
    "all_path_list length: 5176\n",
    "0 : 945 (0, 7) -> (944, 13)\n",
    "0 : 945 (0, 7) -> (944, 16)\n",
    "2 : 945 (0, 16) -> (944, 16)\n",
    "crown_start:78, aorta_end:605, branch_start:107, branch_end:264"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7dc446d2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "46e8dcc2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "79602678",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b4729f88",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/zhoujun-Im33-128/2/  ******\n",
    "sequence length: 253, height: 512, width: 512\n",
    "find spine area: 560.5\n",
    "times:0.5499999999999999\n",
    "all_path_list length: 1503\n",
    "0 : 187 (28, 10) -> (214, 7)\n",
    "1 : 166 (31, 7) -> (196, 7)\n",
    "2 : 165 (31, 7) -> (195, 5)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/zhouzheyu-Im32-152/2/  ******\n",
    "sequence length: 703, height: 512, width: 512\n",
    "find spine area: 598.0\n",
    "times:0.25\n",
    "all_path_list length: 3650\n",
    "0 : 491 (49, 17) -> (539, 17)\n",
    "1 : 491 (49, 17) -> (539, 15)\n",
    "2 : 487 (49, 17) -> (535, 7)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/zhudehong-Im22-91/2/  ******\n",
    "sequence length: 650, height: 512, width: 512\n",
    "find spine area: 2422.0\n",
    "times:0.44999999999999996\n",
    "all_path_list length: 2147\n",
    "0 : 529 (82, 7) -> (610, 8)\n",
    "1 : 454 (164, 10) -> (617, 7)\n",
    "2 : 410 (83, 3) -> (492, 5)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/zhuyongbu-im23-92/2/  ******\n",
    "sequence length: 308, height: 666, width: 512\n",
    "find spine area: 4623.0\n",
    "times:0.6\n",
    "all_path_list length: 1161\n",
    "0 : 128 (30, 8) -> (157, 3)\n",
    "1 : 127 (30, 8) -> (156, 7)\n",
    "2 : 127 (30, 8) -> (156, 6)\n",
    "all_path_list length: 2264\n",
    "0 : 245 (11, 17) -> (255, 6)\n",
    "1 : 243 (11, 17) -> (253, 10)\n",
    "2 : 219 (35, 18) -> (253, 11)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/chengxuejun-Im17-83/2/  ******\n",
    "sequence length: 122, height: 512, width: 512\n",
    "find spine area: 787.5\n",
    "times:0.6\n",
    "all_path_list length: 1017\n",
    "0 : 59 (28, 11) -> (86, 11)\n",
    "1 : 59 (28, 8) -> (86, 11)\n",
    "2 : 55 (5, 7) -> (59, 15)\n",
    "all_path_list length: 1644\n",
    "0 : 85 (5, 12) -> (89, 13)\n",
    "1 : 65 (25, 14) -> (89, 9)\n",
    "2 : 61 (5, 12) -> (65, 7)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/chuguangzhou-Im46-111/2/  ******\n",
    "sequence length: 698, height: 512, width: 512\n",
    "find spine area: 798.0\n",
    "times:0.49999999999999994\n",
    "all_path_list length: 1514\n",
    "0 : 142 (94, 2) -> (235, 1)\n",
    "1 : 139 (125, 3) -> (263, 8)\n",
    "2 : 139 (261, 7) -> (399, 9)\n",
    "all_path_list length: 2414\n",
    "0 : 234 (300, 7) -> (533, 2)\n",
    "1 : 227 (471, 2) -> (697, 3)\n",
    "2 : 223 (300, 5) -> (522, 6)\n",
    "all_path_list length: 3814\n",
    "0 : 490 (44, 12) -> (533, 2)\n",
    "1 : 464 (44, 12) -> (507, 5)\n",
    "2 : 444 (79, 3) -> (522, 8)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/dongxuesheng-Im37-149/2/  ******\n",
    "sequence length: 684, height: 512, width: 512\n",
    "find spine area: 3695.0\n",
    "times:0.15000000000000002\n",
    "all_path_list length: 2341\n",
    "0 : 452 (0, 13) -> (451, 8)\n",
    "1 : 408 (44, 8) -> (451, 4)\n",
    "2 : 304 (44, 8) -> (347, 9)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/durenxi-Im57-172/2/  ******\n",
    "sequence length: 723, height: 512, width: 512\n",
    "find spine area: 5924.5\n",
    "times:0.15000000000000002\n",
    "all_path_list length: 1962\n",
    "0 : 485 (88, 3) -> (572, 2)\n",
    "1 : 484 (88, 2) -> (571, 2)\n",
    "2 : 475 (100, 7) -> (574, 1)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/fangcaiyu-Im21-91/2/  ******\n",
    "sequence length: 267, height: 512, width: 512\n",
    "find spine area: 1856.5\n",
    "times:0.35\n",
    "all_path_list length: 1606\n",
    "0 : 207 (0, 6) -> (206, 6)\n",
    "1 : 193 (0, 6) -> (192, 15)\n",
    "2 : 187 (7, 12) -> (193, 9)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/fengyonghua-Im21-89/2/  ******\n",
    "sequence length: 255, height: 512, width: 512\n",
    "find spine area: 843.5\n",
    "times:0.5499999999999999\n",
    "all_path_list length: 1644\n",
    "0 : 187 (5, 7) -> (191, 10)\n",
    "1 : 178 (14, 10) -> (191, 6)\n",
    "2 : 154 (5, 7) -> (158, 8)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/fuhuajin-Im29-126/2/  ******\n",
    "sequence length: 666, height: 512, width: 512\n",
    "find spine area: 2127.0\n",
    "times:0.44999999999999996\n",
    "all_path_list length: 2853\n",
    "0 : 379 (196, 5) -> (574, 7)\n",
    "1 : 370 (196, 5) -> (565, 5)\n",
    "2 : 341 (196, 5) -> (536, 11)\n",
    "all_path_list length: 4519\n",
    "0 : 481 (185, 11) -> (665, 2)\n",
    "1 : 381 (185, 11) -> (565, 9)\n",
    "2 : 352 (185, 11) -> (536, 12)\n",
    "all_path_list length: 6012\n",
    "0 : 506 (160, 11) -> (665, 4)\n",
    "1 : 483 (160, 11) -> (642, 4)\n",
    "2 : 406 (160, 11) -> (565, 10)\n",
    "all_path_list length: 7247\n",
    "0 : 643 (23, 13) -> (665, 6)\n",
    "1 : 620 (23, 13) -> (642, 7)\n",
    "2 : 543 (23, 13) -> (565, 10)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/gaomingsheng-Im34-151/2/  ******\n",
    "sequence length: 720, height: 512, width: 512\n",
    "find spine area: 1020.0\n",
    "times:0.35\n",
    "all_path_list length: 2577\n",
    "0 : 533 (82, 8) -> (614, 10)\n",
    "1 : 479 (67, 9) -> (545, 16)\n",
    "2 : 439 (67, 9) -> (505, 7)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/gengguorui-Im18-66/2/  ******\n",
    "sequence length: 128, height: 512, width: 512\n",
    "find spine area: 737.0\n",
    "times:0.5499999999999999\n",
    "all_path_list length: 665\n",
    "0 : 59 (28, 7) -> (86, 4)\n",
    "1 : 59 (28, 7) -> (86, 5)\n",
    "2 : 58 (28, 6) -> (85, 10)\n",
    "all_path_list length: 1145\n",
    "0 : 60 (25, 7) -> (84, 11)\n",
    "1 : 60 (25, 7) -> (84, 12)\n",
    "2 : 60 (25, 4) -> (84, 9)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/gengyongchao-Im21-83/2/  ******\n",
    "sequence length: 244, height: 512, width: 512\n",
    "find spine area: 1458.0\n",
    "times:0.6\n",
    "all_path_list length: 1952\n",
    "0 : 90 (3, 10) -> (92, 10)\n",
    "1 : 83 (10, 8) -> (92, 10)\n",
    "2 : 69 (124, 14) -> (192, 6)\n",
    "all_path_list length: 3428\n",
    "0 : 92 (1, 15) -> (92, 20)\n",
    "1 : 90 (3, 13) -> (92, 11)\n",
    "2 : 86 (3, 13) -> (88, 17)\n",
    "all_path_list length: 4458\n",
    "0 : 92 (1, 17) -> (92, 22)\n",
    "1 : 91 (3, 13) -> (93, 13)\n",
    "2 : 90 (3, 13) -> (92, 11)\n",
    "all_path_list length: 5472\n",
    "0 : 92 (1, 18) -> (92, 23)\n",
    "1 : 91 (3, 16) -> (93, 13)\n",
    "2 : 90 (3, 16) -> (92, 11)\n",
    "all_path_list length: 6533\n",
    "0 : 92 (1, 24) -> (92, 27)\n",
    "1 : 91 (3, 21) -> (93, 14)\n",
    "2 : 90 (3, 21) -> (92, 12)\n",
    "all_path_list length: 7525\n",
    "0 : 92 (1, 23) -> (92, 27)\n",
    "1 : 91 (3, 22) -> (93, 16)\n",
    "2 : 90 (3, 22) -> (92, 13)\n",
    "all_path_list length: 8310\n",
    "0 : 92 (1, 25) -> (92, 27)\n",
    "1 : 91 (3, 22) -> (93, 16)\n",
    "2 : 90 (3, 22) -> (92, 13)\n",
    "all_path_list length: 8692\n",
    "0 : 92 (1, 28) -> (92, 27)\n",
    "1 : 91 (3, 22) -> (93, 16)\n",
    "2 : 90 (3, 22) -> (92, 13)\n",
    "Can't find aorta!\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/hebin-Im41-91/2/  ******\n",
    "sequence length: 270, height: 512, width: 512\n",
    "find spine area: 1415.0\n",
    "times:0.3\n",
    "all_path_list length: 1489\n",
    "0 : 189 (33, 14) -> (221, 2)\n",
    "1 : 150 (44, 10) -> (193, 10)\n",
    "2 : 144 (45, 11) -> (188, 5)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/hejianjun-Im31-99/2/  ******\n",
    "sequence length: 639, height: 512, width: 512\n",
    "find spine area: 1908.0\n",
    "times:0.6\n",
    "all_path_list length: 2945\n",
    "0 : 544 (20, 11) -> (563, 7)\n",
    "1 : 532 (32, 18) -> (563, 8)\n",
    "2 : 500 (32, 18) -> (531, 9)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/heshengnan-Im16-90/2/  ******\n",
    "sequence length: 253, height: 512, width: 512\n",
    "find spine area: 549.0\n",
    "times:0.49999999999999994\n",
    "all_path_list length: 1870\n",
    "0 : 185 (2, 4) -> (186, 11)\n",
    "1 : 180 (2, 4) -> (181, 15)\n",
    "2 : 179 (3, 5) -> (181, 8)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/huangjianhua-Im38-144/2/  ******\n",
    "sequence length: 648, height: 512, width: 512\n",
    "find spine area: 2561.0\n",
    "times:0.25\n",
    "all_path_list length: 2087\n",
    "0 : 315 (236, 3) -> (550, 5)\n",
    "1 : 310 (242, 1) -> (551, 15)\n",
    "2 : 250 (242, 1) -> (491, 6)\n",
    "all_path_list length: 3672\n",
    "0 : 570 (30, 10) -> (599, 6)\n",
    "1 : 560 (30, 10) -> (589, 17)\n",
    "2 : 446 (30, 8) -> (475, 4)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/hujiaguo-Im34-58/2/  ******\n",
    "sequence length: 666, height: 512, width: 512\n",
    "find spine area: 573.5\n",
    "times:0.39999999999999997\n",
    "all_path_list length: 2485\n",
    "0 : 554 (35, 14) -> (588, 11)\n",
    "1 : 508 (57, 4) -> (564, 3)\n",
    "2 : 500 (35, 14) -> (534, 9)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/jiangquanfeng-Im 50-107/2/  ******\n",
    "sequence length: 537, height: 512, width: 512\n",
    "find spine area: 1497.0\n",
    "times:0.6\n",
    "all_path_list length: 1647\n",
    "0 : 310 (0, 7) -> (309, 10)\n",
    "1 : 309 (0, 6) -> (308, 10)\n",
    "2 : 308 (0, 4) -> (307, 7)\n",
    "all_path_list length: 2871\n",
    "0 : 525 (0, 13) -> (524, 5)\n",
    "1 : 444 (0, 9) -> (443, 8)\n",
    "2 : 413 (0, 13) -> (412, 10)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/litingxian-Im36-141/2/  ******\n",
    "sequence length: 618, height: 512, width: 512\n",
    "find spine area: 804.5\n",
    "times:0.6\n",
    "all_path_list length: 1899\n",
    "0 : 137 (423, 10) -> (559, 3)\n",
    "1 : 136 (423, 10) -> (558, 2)\n",
    "2 : 126 (108, 2) -> (233, 0)\n",
    "all_path_list length: 3143\n",
    "0 : 359 (259, 10) -> (617, 11)\n",
    "1 : 358 (125, 5) -> (482, 5)\n",
    "2 : 358 (124, 4) -> (481, 5)\n",
    "all_path_list length: 5042\n",
    "0 : 566 (52, 12) -> (617, 15)\n",
    "1 : 566 (52, 12) -> (617, 18)\n",
    "2 : 481 (29, 9) -> (509, 13)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/luohuafa-im29-96/2/  ******\n",
    "sequence length: 323, height: 666, width: 512\n",
    "find spine area: 1336.5\n",
    "times:0.25\n",
    "all_path_list length: 1435\n",
    "0 : 316 (0, 11) -> (315, 9)\n",
    "1 : 275 (0, 11) -> (274, 9)\n",
    "2 : 236 (4, 6) -> (239, 6)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/lushida-Im33-159/2/  ******\n",
    "sequence length: 705, height: 512, width: 512\n",
    "find spine area: 712.5\n",
    "times:0.39999999999999997\n",
    "all_path_list length: 2725\n",
    "0 : 377 (105, 2) -> (481, 10)\n",
    "1 : 364 (119, 4) -> (482, 5)\n",
    "2 : 360 (123, 1) -> (482, 6)\n",
    "all_path_list length: 3914\n",
    "0 : 389 (105, 2) -> (493, 13)\n",
    "1 : 375 (119, 4) -> (493, 4)\n",
    "2 : 372 (123, 1) -> (494, 4)\n",
    "all_path_list length: 5072\n",
    "0 : 600 (105, 3) -> (704, 22)\n",
    "1 : 592 (105, 3) -> (696, 16)\n",
    "2 : 570 (105, 3) -> (674, 12)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/lvpeiji-Im32-136/2/  ******\n",
    "sequence length: 636, height: 512, width: 512\n",
    "find spine area: 786.0\n",
    "times:0.44999999999999996\n",
    "all_path_list length: 2142\n",
    "0 : 431 (0, 10) -> (430, 15)\n",
    "1 : 430 (0, 6) -> (429, 5)\n",
    "2 : 396 (33, 4) -> (428, 2)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/manyanfeng-Im32-153/2/  ******\n",
    "sequence length: 660, height: 512, width: 512\n",
    "find spine area: 620.0\n",
    "times:0.25\n",
    "all_path_list length: 3071\n",
    "0 : 367 (0, 9) -> (366, 18)\n",
    "1 : 365 (0, 9) -> (364, 14)\n",
    "2 : 359 (0, 9) -> (358, 18)\n",
    "all_path_list length: 4422\n",
    "0 : 660 (0, 10) -> (659, 20)\n",
    "1 : 633 (0, 10) -> (632, 9)\n",
    "2 : 528 (0, 10) -> (527, 8)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/maoliang-Im12-90/2/  ******\n",
    "sequence length: 253, height: 512, width: 512\n",
    "find spine area: 822.5\n",
    "times:0.2\n",
    "all_path_list length: 1497\n",
    "0 : 196 (5, 10) -> (200, 7)\n",
    "1 : 184 (17, 3) -> (200, 6)\n",
    "2 : 152 (17, 3) -> (168, 15)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/modehe-Im34-89/2/  ******\n",
    "sequence length: 301, height: 512, width: 512\n",
    "find spine area: 2515.5\n",
    "times:0.44999999999999996\n",
    "all_path_list length: 1424\n",
    "0 : 265 (36, 18) -> (300, 9)\n",
    "1 : 264 (37, 15) -> (300, 10)\n",
    "2 : 264 (37, 14) -> (300, 10)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/nibingshan-Im33-42/2/  ******\n",
    "sequence length: 696, height: 512, width: 512\n",
    "find spine area: 584.5\n",
    "times:0.5499999999999999\n",
    "all_path_list length: 2646\n",
    "0 : 601 (0, 9) -> (600, 3)\n",
    "1 : 583 (17, 5) -> (599, 3)\n",
    "2 : 552 (0, 9) -> (551, 5)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/qiquanping-Im25-54/2/  ******\n",
    "sequence length: 1073, height: 512, width: 512\n",
    "find spine area: 679.5\n",
    "times:0.5499999999999999\n",
    "all_path_list length: 4227\n",
    "0 : 698 (0, 12) -> (697, 7)\n",
    "1 : 594 (0, 12) -> (593, 18)\n",
    "2 : 568 (0, 12) -> (567, 12)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/shengfumei-Im31-132/2/  ******\n",
    "sequence length: 612, height: 512, width: 512\n",
    "find spine area: 740.5\n",
    "times:0.3\n",
    "all_path_list length: 2601\n",
    "0 : 514 (33, 16) -> (546, 10)\n",
    "1 : 504 (41, 6) -> (544, 5)\n",
    "2 : 482 (61, 3) -> (542, 2)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/shenhuagou-Im33-95/2/  ******\n",
    "sequence length: 633, height: 512, width: 512\n",
    "find spine area: 533.5\n",
    "times:0.44999999999999996\n",
    "all_path_list length: 2417\n",
    "0 : 486 (30, 13) -> (515, 5)\n",
    "1 : 480 (32, 9) -> (511, 3)\n",
    "2 : 466 (30, 13) -> (495, 11)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/shenjiaren-Im35-136/2/  ******\n",
    "sequence length: 255, height: 512, width: 512\n",
    "find spine area: 1455.0\n",
    "times:0.49999999999999994\n",
    "all_path_list length: 1768\n",
    "0 : 161 (23, 5) -> (183, 10)\n",
    "1 : 152 (32, 10) -> (183, 11)\n",
    "2 : 113 (23, 5) -> (135, 18)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/shenjie-Im36-100/2/  ******\n",
    "sequence length: 715, height: 512, width: 512\n",
    "find spine area: 4348.5\n",
    "times:0.35\n",
    "all_path_list length: 1839\n",
    "0 : 298 (298, 5) -> (595, 13)\n",
    "1 : 274 (298, 5) -> (571, 2)\n",
    "2 : 272 (300, 4) -> (571, 2)\n",
    "all_path_list length: 3282\n",
    "0 : 607 (59, 11) -> (665, 6)\n",
    "1 : 606 (59, 11) -> (664, 9)\n",
    "2 : 602 (59, 11) -> (660, 9)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/shiaihua-Im28-108/2/  ******\n",
    "sequence length: 643, height: 512, width: 512\n",
    "find spine area: 733.0\n",
    "times:0.35\n",
    "all_path_list length: 2599\n",
    "0 : 427 (42, 8) -> (468, 10)\n",
    "1 : 416 (42, 8) -> (457, 11)\n",
    "2 : 395 (43, 4) -> (437, 5)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/shilei-Im21-92/2/  ******\n",
    "sequence length: 1129, height: 512, width: 512\n",
    "find spine area: 667.0\n",
    "times:0.6\n",
    "all_path_list length: 3589\n",
    "0 : 954 (64, 15) -> (1017, 7)\n",
    "1 : 952 (64, 6) -> (1015, 4)\n",
    "2 : 835 (64, 15) -> (898, 4)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/shuzhiming-Im28-80/2/  ******\n",
    "sequence length: 129, height: 512, width: 512\n",
    "find spine area: 692.5\n",
    "find spine area: 2.0\n",
    "times:0.3\n",
    "all_path_list length: 853\n",
    "0 : 92 (10, 10) -> (101, 9)\n",
    "1 : 88 (10, 10) -> (97, 8)\n",
    "2 : 64 (14, 8) -> (77, 8)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/taofangguo-Im30-142/2/  ******\n",
    "sequence length: 639, height: 512, width: 512\n",
    "find spine area: 639.0\n",
    "times:0.3\n",
    "all_path_list length: 2749\n",
    "0 : 406 (41, 7) -> (446, 9)\n",
    "1 : 393 (36, 14) -> (428, 15)\n",
    "2 : 390 (40, 14) -> (429, 7)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/wangaishui-Im75-128/2/  ******\n",
    "sequence length: 711, height: 512, width: 512\n",
    "find spine area: 903.0\n",
    "times:0.3\n",
    "all_path_list length: 2157\n",
    "0 : 680 (20, 9) -> (699, 10)\n",
    "1 : 539 (20, 9) -> (558, 6)\n",
    "2 : 492 (89, 7) -> (580, 10)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/wanggangxiang-Im16-90/2/  ******\n",
    "sequence length: 745, height: 512, width: 512\n",
    "find spine area: 559.0\n",
    "times:0.5499999999999999\n",
    "all_path_list length: 2720\n",
    "0 : 474 (131, 10) -> (604, 6)\n",
    "1 : 454 (131, 10) -> (584, 8)\n",
    "2 : 448 (131, 10) -> (578, 8)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/wanggaosheng-Im24-57/2/  ******\n",
    "sequence length: 135, height: 512, width: 512\n",
    "find spine area: 906.5\n",
    "times:0.15000000000000002\n",
    "all_path_list length: 756\n",
    "0 : 97 (11, 10) -> (107, 10)\n",
    "1 : 94 (14, 10) -> (107, 7)\n",
    "2 : 92 (14, 14) -> (105, 8)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/wangjianjun-Im30-110/2/  ******\n",
    "sequence length: 630, height: 512, width: 512\n",
    "find spine area: 3041.5\n",
    "times:0.3\n",
    "all_path_list length: 1619\n",
    "0 : 548 (0, 11) -> (547, 7)\n",
    "1 : 502 (0, 11) -> (501, 4)\n",
    "2 : 487 (0, 11) -> (486, 14)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/wanglianlian-Im42-139/2/  ******\n",
    "sequence length: 690, height: 512, width: 512\n",
    "find spine area: 3090.5\n",
    "times:0.15000000000000002\n",
    "all_path_list length: 1705\n",
    "0 : 269 (279, 1) -> (547, 1)\n",
    "1 : 268 (281, 4) -> (548, 3)\n",
    "2 : 249 (281, 4) -> (529, 2)\n",
    "all_path_list length: 2840\n",
    "0 : 311 (261, 5) -> (571, 10)\n",
    "1 : 310 (261, 5) -> (570, 6)\n",
    "2 : 269 (261, 5) -> (529, 6)\n",
    "all_path_list length: 4316\n",
    "0 : 655 (35, 18) -> (689, 25)\n",
    "1 : 592 (35, 18) -> (626, 11)\n",
    "2 : 542 (69, 6) -> (610, 8)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/wangmanying-Im23-134/2/  ******\n",
    "sequence length: 588, height: 512, width: 512\n",
    "find spine area: 2113.5\n",
    "find spine area: 590.0\n",
    "times:0.35\n",
    "all_path_list length: 2237\n",
    "0 : 232 (0, 7) -> (231, 9)\n",
    "1 : 228 (0, 7) -> (227, 10)\n",
    "2 : 224 (8, 9) -> (231, 6)\n",
    "all_path_list length: 3385\n",
    "0 : 439 (0, 7) -> (438, 11)\n",
    "1 : 412 (0, 7) -> (411, 13)\n",
    "2 : 409 (0, 7) -> (408, 11)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/wangzhongwen-Im36-80/2/  ******\n",
    "sequence length: 133, height: 512, width: 512\n",
    "find spine area: 4727.0\n",
    "times:0.35\n",
    "all_path_list length: 738\n",
    "0 : 95 (11, 9) -> (105, 4)\n",
    "1 : 91 (15, 8) -> (105, 4)\n",
    "2 : 91 (15, 9) -> (105, 3)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/wuguangde-im55-205/2/  ******\n",
    "sequence length: 299, height: 512, width: 512\n",
    "find spine area: 2193.5\n",
    "find spine area: 251.5\n",
    "find spine area: 156.5\n",
    "times:0.35\n",
    "all_path_list length: 1565\n",
    "0 : 195 (61, 9) -> (255, 4)\n",
    "1 : 173 (61, 11) -> (233, 4)\n",
    "2 : 170 (61, 9) -> (230, 12)\n",
    "all_path_list length: 2517\n",
    "0 : 202 (59, 14) -> (260, 12)\n",
    "1 : 201 (61, 21) -> (261, 14)\n",
    "2 : 177 (59, 14) -> (235, 13)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/xiewenbo-Im25-40/2/  ******\n",
    "sequence length: 1033, height: 512, width: 512\n",
    "find spine area: 532.5\n",
    "times:0.6\n",
    "all_path_list length: 3289\n",
    "0 : 898 (10, 9) -> (907, 15)\n",
    "1 : 892 (15, 5) -> (906, 2)\n",
    "2 : 862 (26, 4) -> (887, 1)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/xujinbiao-Im37-146/2/  ******\n",
    "sequence length: 651, height: 512, width: 512\n",
    "find spine area: 804.5\n",
    "times:0.35\n",
    "all_path_list length: 2794\n",
    "0 : 510 (75, 9) -> (584, 13)\n",
    "1 : 438 (75, 9) -> (512, 12)\n",
    "2 : 387 (75, 9) -> (461, 6)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/xujinyang-Im15-30/2/  ******\n",
    "sequence length: 139, height: 512, width: 512\n",
    "find spine area: 1226.5\n",
    "times:0.35\n",
    "all_path_list length: 889\n",
    "0 : 94 (10, 7) -> (103, 5)\n",
    "1 : 93 (10, 7) -> (102, 3)\n",
    "2 : 91 (10, 10) -> (100, 7)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/yangwenbin-Im44-125/2/  ******\n",
    "sequence length: 699, height: 512, width: 512\n",
    "find spine area: 537.0\n",
    "times:0.3\n",
    "all_path_list length: 1951\n",
    "0 : 210 (87, 10) -> (296, 10)\n",
    "1 : 196 (87, 10) -> (282, 13)\n",
    "2 : 194 (87, 10) -> (280, 7)\n",
    "all_path_list length: 3241\n",
    "0 : 243 (23, 8) -> (265, 9)\n",
    "1 : 235 (23, 8) -> (257, 2)\n",
    "2 : 234 (23, 8) -> (256, 5)\n",
    "all_path_list length: 4401\n",
    "0 : 274 (417, 7) -> (690, 9)\n",
    "1 : 258 (0, 13) -> (257, 8)\n",
    "2 : 256 (417, 7) -> (672, 5)\n",
    "all_path_list length: 5490\n",
    "0 : 517 (0, 12) -> (516, 17)\n",
    "1 : 485 (0, 12) -> (484, 22)\n",
    "2 : 473 (0, 12) -> (472, 16)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/yangyoufu-Im16-25/2/  ******\n",
    "sequence length: 122, height: 512, width: 512\n",
    "find spine area: 1817.0\n",
    "times:0.3\n",
    "all_path_list length: 1084\n",
    "0 : 65 (0, 8) -> (64, 8)\n",
    "1 : 60 (5, 11) -> (64, 6)\n",
    "2 : 58 (7, 15) -> (64, 20)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/yaoliang-Im30-83/2/  ******\n",
    "sequence length: 289, height: 512, width: 512\n",
    "find spine area: 3250.0\n",
    "times:0.05000000000000002\n",
    "all_path_list length: 1549\n",
    "0 : 88 (89, 9) -> (176, 17)\n",
    "1 : 81 (132, 13) -> (212, 10)\n",
    "2 : 79 (89, 9) -> (167, 19)\n",
    "all_path_list length: 2216\n",
    "0 : 99 (190, 3) -> (288, 6)\n",
    "1 : 93 (87, 17) -> (179, 10)\n",
    "2 : 89 (190, 3) -> (278, 7)\n",
    "all_path_list length: 2818\n",
    "0 : 183 (25, 16) -> (207, 7)\n",
    "1 : 177 (31, 10) -> (207, 6)\n",
    "2 : 159 (31, 9) -> (189, 8)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/yinghao-Im18-86/2/  ******\n",
    "sequence length: 215, height: 666, width: 512\n",
    "find spine area: 1762.5\n",
    "times:0.3\n",
    "all_path_list length: 1613\n",
    "0 : 185 (0, 5) -> (184, 4)\n",
    "1 : 171 (0, 5) -> (170, 14)\n",
    "2 : 156 (0, 5) -> (155, 15)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/yingzhemin-Im58-108/2/  ******\n",
    "sequence length: 277, height: 512, width: 512\n",
    "find spine area: 1606.0\n",
    "times:0.2\n",
    "all_path_list length: 1364\n",
    "0 : 196 (11, 10) -> (206, 4)\n",
    "1 : 195 (11, 10) -> (205, 4)\n",
    "2 : 152 (125, 5) -> (276, 5)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/yuanenchun-Im27-93/2/  ******\n",
    "sequence length: 279, height: 512, width: 512\n",
    "find spine area: 1754.0\n",
    "times:0.2\n",
    "all_path_list length: 1910\n",
    "0 : 189 (26, 10) -> (214, 3)\n",
    "1 : 172 (29, 7) -> (200, 8)\n",
    "2 : 170 (26, 10) -> (195, 13)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/yujinmin-Im25-78/2/  ******\n",
    "sequence length: 977, height: 512, width: 512\n",
    "find spine area: 1882.0\n",
    "times:0.3\n",
    "all_path_list length: 2561\n",
    "0 : 738 (15, 3) -> (752, 5)\n",
    "1 : 703 (15, 3) -> (717, 7)\n",
    "2 : 700 (53, 8) -> (752, 5)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/yukongban-Im30-139/2/  ******\n",
    "sequence length: 636, height: 512, width: 512\n",
    "find spine area: 782.0\n",
    "times:0.49999999999999994\n",
    "all_path_list length: 2987\n",
    "0 : 504 (15, 10) -> (518, 4)\n",
    "1 : 495 (22, 13) -> (516, 0)\n",
    "2 : 486 (31, 5) -> (516, 0)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/yuliyao-Im25-31/2/  ******\n",
    "sequence length: 672, height: 512, width: 512\n",
    "find spine area: 993.0\n",
    "times:0.39999999999999997\n",
    "all_path_list length: 2724\n",
    "0 : 451 (35, 5) -> (485, 8)\n",
    "1 : 449 (36, 5) -> (484, 12)\n",
    "2 : 448 (36, 5) -> (483, 7)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/zengzhengyou-Im54-145/2/  ******\n",
    "sequence length: 663, height: 512, width: 512\n",
    "find spine area: 2725.5\n",
    "times:0.6\n",
    "all_path_list length: 2158\n",
    "0 : 226 (268, 2) -> (493, 13)\n",
    "1 : 226 (38, 6) -> (263, 3)\n",
    "2 : 222 (268, 2) -> (489, 17)\n",
    "all_path_list length: 3472\n",
    "0 : 494 (0, 9) -> (493, 13)\n",
    "1 : 490 (0, 9) -> (489, 19)\n",
    "2 : 483 (11, 14) -> (493, 9)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/zhangxinliang-Im16-24/2/  ******\n",
    "sequence length: 58, height: 512, width: 512\n",
    "find spine area: 1193.0\n",
    "find spine area: 388.0\n",
    "times:0.49999999999999994\n",
    "all_path_list length: 449\n",
    "0 : 51 (7, 13) -> (57, 10)\n",
    "1 : 51 (7, 8) -> (57, 11)\n",
    "2 : 27 (31, 10) -> (57, 10)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/zhangxiyuan-Im9-50/2/  ******\n",
    "sequence length: 221, height: 512, width: 512\n",
    "find spine area: 4452.0\n",
    "times:0.10000000000000002\n",
    "all_path_list length: 991\n",
    "0 : 155 (10, 5) -> (164, 10)\n",
    "1 : 152 (10, 5) -> (161, 6)\n",
    "2 : 146 (10, 5) -> (155, 11)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/zhaojianbin-Im31-158/2/  ******\n",
    "sequence length: 669, height: 512, width: 512\n",
    "find spine area: 2084.5\n",
    "times:0.35\n",
    "all_path_list length: 2890\n",
    "0 : 250 (0, 11) -> (249, 8)\n",
    "1 : 240 (0, 11) -> (239, 10)\n",
    "2 : 225 (0, 11) -> (224, 7)\n",
    "all_path_list length: 4495\n",
    "0 : 531 (0, 12) -> (530, 15)\n",
    "1 : 501 (30, 9) -> (530, 7)\n",
    "2 : 489 (0, 12) -> (488, 9)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/zhaojun-Im45-103/2/  ******\n",
    "sequence length: 632, height: 512, width: 512\n",
    "find spine area: 17894.5\n",
    "find spine area: 808.0\n",
    "find spine area: 17.5\n",
    "find spine area: 5.5\n",
    "find spine area: 1.0\n",
    "find spine area: 25370.0\n",
    "times:0.10000000000000002\n",
    "all_path_list length: 2179\n",
    "0 : 229 (0, 7) -> (228, 3)\n",
    "1 : 191 (281, 4) -> (471, 8)\n",
    "2 : 190 (38, 9) -> (227, 3)\n",
    "all_path_list length: 3573\n",
    "0 : 251 (0, 12) -> (250, 9)\n",
    "1 : 251 (0, 12) -> (250, 11)\n",
    "2 : 225 (262, 3) -> (486, 6)\n",
    "all_path_list length: 4853\n",
    "0 : 509 (0, 18) -> (508, 13)\n",
    "1 : 504 (0, 18) -> (503, 13)\n",
    "2 : 497 (0, 18) -> (496, 11)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/zhengsiqing-Im35-106/2/  ******\n",
    "sequence length: 666, height: 512, width: 512\n",
    "find spine area: 1991.0\n",
    "times:0.3\n",
    "all_path_list length: 3064\n",
    "0 : 348 (31, 12) -> (378, 9)\n",
    "1 : 339 (31, 12) -> (369, 12)\n",
    "2 : 336 (31, 12) -> (366, 11)\n",
    "all_path_list length: 4558\n",
    "0 : 368 (245, 24) -> (612, 22)\n",
    "1 : 368 (245, 24) -> (612, 24)\n",
    "2 : 366 (31, 13) -> (396, 13)\n",
    "all_path_list length: 5834\n",
    "0 : 635 (31, 14) -> (665, 21)\n",
    "1 : 630 (31, 14) -> (660, 18)\n",
    "2 : 596 (45, 22) -> (640, 14)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/zhoufei-Im16-97/2/  ******\n",
    "sequence length: 581, height: 512, width: 512\n",
    "find spine area: 1656.0\n",
    "times:0.49999999999999994\n",
    "all_path_list length: 3360\n",
    "0 : 466 (0, 4) -> (465, 2)\n",
    "1 : 412 (0, 4) -> (411, 6)\n",
    "2 : 385 (0, 4) -> (384, 9)\n",
    "******Processing: /nfs3-p1/zsxm/dataset/aorta/zhoujinhai-Im14-94/2/  ******\n",
    "sequence length: 945, height: 512, width: 512\n",
    "find spine area: 2807.5\n",
    "times:0.44999999999999996\n",
    "all_path_list length: 3558\n",
    "0 : 875 (0, 9) -> (874, 5)\n",
    "1 : 874 (0, 5) -> (873, 1)\n",
    "2 : 821 (0, 9) -> (820, 6)\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5fc0a1ea",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "for p in range(len(input_folder_list)):\n",
    "    print(f'******Processing: {input_folder_list[p]}  ******')\n",
    "    patient = load_scan(input_folder_list[p])\n",
    "    patient_pixels = get_pixels_hu(patient)\n",
    "    img_height, img_width = patient_pixels.shape[1:3]\n",
    "    print(f'sequence length: {patient_pixels.shape[0]}, height: {img_height}, width: {img_width}')\n",
    "    spine_x, spine_y = 0, 0\n",
    "    \n",
    "    patient_sum = np.sum(patient_pixels, axis=0)\n",
    "    patient_sum = patient_sum.astype(np.float32)\n",
    "    max_val = patient_sum.max()\n",
    "    times = 0.6\n",
    "    while times > 0:\n",
    "        thr, patient_thr = cv2.threshold(patient_sum, max_val*times, 255, cv2.THRESH_BINARY)\n",
    "        patient_thr = patient_thr.astype(np.uint8)\n",
    "        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))\n",
    "        patient_thr = cv2.erode(patient_thr, kernel, iterations=2)\n",
    "        sum_contours, _ = cv2.findContours(patient_thr, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)\n",
    "        sum_contours_list = [(cv2.contourArea(contour), contour) for contour in sum_contours]\n",
    "        sum_contours_list.sort(key=lambda x: x[0], reverse=True)\n",
    "        if len(sum_contours_list) == 0 \\\n",
    "        or len(sum_contours_list) > 1 and sum_contours_list[0][0] < 2 * sum_contours_list[1][0] \\\n",
    "        or sum_contours_list[0][0] < img_height*img_width*0.002:\n",
    "            times -= 0.05\n",
    "            continue\n",
    "        find_flag = False\n",
    "        for area, contour in sum_contours_list:\n",
    "            print(f'find spine area: {area}')\n",
    "            x, y, w, h = cv2.boundingRect(contour)\n",
    "            cx, cy = x+w//2, y+h//2\n",
    "            if img_height/2 < cy < img_height*0.75 and img_width*0.375 < cx < img_width*0.625:\n",
    "                spine_x, spine_y = cx, cy\n",
    "                find_flag = True\n",
    "                break\n",
    "        if find_flag:\n",
    "            print(f'times:{times}')\n",
    "            break\n",
    "        times -= 0.05\n",
    "\n",
    "    if spine_x == 0 or spine_y == 0:\n",
    "        raise Exception(f'spine_x:{spine_x}, spine_y:{spine_y}')\n",
    "            \n",
    "    all_path_list = []\n",
    "    for t in range(len(set_window_list)):\n",
    "        patient_copy = set_window_list[t](patient_pixels, 300, 600)\n",
    "        start_x = int(spine_x-img_width*0.15)\n",
    "        end_x = int(spine_x+img_width*0.15)\n",
    "        start_y = int(spine_y-img_height*0.4)\n",
    "        end_y = int(spine_y+img_height*0.1)\n",
    "        patient_cut = patient_copy[:, start_y:end_y, start_x:end_x]\n",
    "\n",
    "        cut_height, cut_width = patient_cut.shape[1:3]\n",
    "        cut_min_side = min(cut_height, cut_width)\n",
    "        pre_contours, _ = cv2.findContours(patient_cut[0], cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)\n",
    "        pre_circle = list(map(lambda x: cv2.minEnclosingCircle(x), pre_contours))\n",
    "        path_dict = {}\n",
    "        for i in range(1, len(patient_cut)):\n",
    "            cur_contours, _ = cv2.findContours(patient_cut[i], cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)\n",
    "            cur_circle = list(map(lambda x: cv2.minEnclosingCircle(x), cur_contours))\n",
    "            for j in range(len(pre_circle)):\n",
    "                if pre_circle[j][0][0] < 0.4*cut_width \\\n",
    "                or pre_circle[j][0][0] < 0.6*cut_width and pre_circle[j][0][1] > 0.8*cut_height:\n",
    "                    continue\n",
    "                candidate_list = []\n",
    "                for k in range(len(cur_circle)):\n",
    "                    if cur_circle[k][0][0] < 0.4*cut_width \\\n",
    "                    or cur_circle[k][0][0] < 0.6*cut_width and cur_circle[k][0][1] > 0.8*cut_height:\n",
    "                        continue\n",
    "                    dis = sqrt((pre_circle[j][0][0]-cur_circle[k][0][0])**2+(pre_circle[j][0][1]-cur_circle[k][0][1])**2)\n",
    "                    max_r, min_r = max(pre_circle[j][1], cur_circle[k][1]), min(pre_circle[j][1], cur_circle[k][1])\n",
    "                    if dis <= max_r:#dis <= max_r - 0.5 * min_r:\n",
    "                        candidate_list.append((k, cv2.contourArea(cur_contours[k])))\n",
    "\n",
    "                if len(candidate_list) == 0:\n",
    "                    continue\n",
    "                candidate_list.sort(key=lambda x:x[1])\n",
    "                path = path_dict.pop((i-1, j), Elem((i-1, j), pre_contours[j]))\n",
    "                temp_path_list = [copy.deepcopy(path) for s in range(len(candidate_list))]\n",
    "                for s, candidate in enumerate(candidate_list):\n",
    "                    k = candidate[0]\n",
    "                    if get_intersection(patient_cut[i], pre_contours[j], cur_contours[k]) < 0.6:\n",
    "                        continue\n",
    "                    temp_path_list[s].append((i, k), cur_contours[k])\n",
    "                    if not (i, k) in path_dict \\\n",
    "                    or len(path_dict[(i, k)]) < len(temp_path_list[s]) \\\n",
    "                    or path_dict[(i, k)].get_prev_area() < temp_path_list[s].get_prev_area():\n",
    "                        path_dict[(i, k)] = temp_path_list[s]\n",
    "\n",
    "            pre_contours = cur_contours\n",
    "            pre_circle = cur_circle\n",
    "\n",
    "        path_list = list(path_dict.values())\n",
    "        path_list.sort(key=lambda x: len(x), reverse=True)\n",
    "        all_path_list += path_list\n",
    "        \n",
    "    print('all_path_list length:', len(all_path_list))\n",
    "    if len(all_path_list) == 0:\n",
    "        print(\"Can't find aorta!\")\n",
    "        continue\n",
    "    all_path_list.sort(key=lambda x: len(x), reverse=True)\n",
    "    for i in range(min(5, len(all_path_list))):\n",
    "        print(i, ':', len(all_path_list[i]), all_path_list[i].root, '->', all_path_list[i].end)\n",
    "        \n",
    "    path = all_path_list[0]\n",
    "    start = path.root[0]\n",
    "    end = path.end[0]+1\n",
    "    \n",
    "    path.get_bboxes()\n",
    "    \n",
    "    patient = load_scan(input_folder_list[p])\n",
    "    draw_bbox(patient, path, start_x, start_y, os.path.join(input_folder_list[p], 'save'))\n",
    "'''"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
